【问题标题】:Heroku Postgres: AWS s3 presigned URL not workingHeroku Postgres:AWS s3 预签名 URL 不起作用
【发布时间】:2021-03-19 08:17:03
【问题描述】:

我犯了这个错误 6 小时后发疯了。

我已经使用 CLI 创建了一个 heroku 应用程序并成功推送了 git,但是我需要在应用程序工作之前使用 db.create_all() 初始化一堆表,但是我的 localhost postgres 中有 6gb 的数据我希望迁移到heroku使用。

以通常的方式 (create_all) 实例化表格将不起作用,因为索引页面的表格需要某些内容,因此我的表格必须预先填充所述本地主机数据才能工作。

我已按照此处的文档进行操作,并导出了我的 .dump 文件并将其上传到我的存储桶:https://devcenter.heroku.com/articles/heroku-postgres-import-export#import

已启用公共共享,我已使用 setx 命令在 AWS CLI 中设置 ACCESS_KEY_IDSECRET_ACCESS_KEY。我也设置了heroku config:set AWS_ACCESS_KEY_ID=matching_aws_cli_blah AWS_SECRET_ACCESS_KEY=matching_aws_cli_blah

然后使用命令 aws s3 presign s3_url 我复制了输出(我将添加看起来与我使用的 AWS 文档 https://awscli.amazonaws.com/v2/documentation/api/latest/reference/s3/presign.html 不同)

我的外观示例(随机更改了一些数字) https://mybucketname.s3.eu-west-2.amazonaws.com/mydb.dump?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=LLLLL4C7LLLLLLLNLLLQ%2FNNNNNN08%2Feu-west-2%2Fs3%2Faws4_request&X-Amz-Date=20201208T004613Z&X-Amz-Expires=3600&X-Amz-SignedHeaders=host&X-Amz-Signature=ll9d80nnnnc457ea83298fdnnnn4a25b0c9ll066f54e6ff8acf42dafnnnea8877 而文档提供了一个漂亮整洁的 AWSAccessKeyId= 变量。

我只有一个存储桶,Block public access (bucket settings) 设置为 OFF,并且文件本身可以公开共享。对象 URL 初始化下载正常,包括隐身。

使用以下命令时(根据 heroku 文档)

heroku pg:backups:restore 'https://mybucketname.s3.eu-west-2.amazonaws.com/mydb.dump?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=LLLLL4C7LLLLLLLNLLLQ%2FNNNNNN08%2Feu-west-2%2Fs3%2Faws4_request&X-Amz-Date=20201208T004613Z&X-Amz-Expires=3600&X-Amz-SignedHeaders=host&X-Amz-Signature=ll9d80nnnnc457ea83298fdnnnn4a25b0c9ll066f54e6ff8acf42dafnnnea8877' DATABASE_URL

我得到以下输出到终端。

Restoring... !
 !    An error occurred and the backup did not finish.
 !
 !    waiting for restore to complete
 !    pg_restore finished with errors
 !    waiting for download to complete
 !    download finished with errors
 !    please check the source URL and ensure it is publicly accessible
 !
 !    Run heroku pg:backups:info r001 for more details.
'X-Amz-Credential' is not recognized as an internal or external command,
operable program or batch file.
'X-Amz-Date' is not recognized as an internal or external command,
operable program or batch file.
'X-Amz-Expires' is not recognized as an internal or external command,
operable program or batch file.
'X-Amz-SignedHeaders' is not recognized as an internal or external command,
operable program or batch file.
'X-Amz-Signature' is not recognized as an internal or external command,
operable program or batch file.

日志文件中显示的

=== Backup r001
Database:         BACKUP
Started at:       2020-12-08 00:47:33 +0000
Finished at:      2020-12-08 00:47:34 +0000
Status:           Failed
Type:             Manual
Backup Size:      0.00B (0% compression)

=== Backup Logs
2020-12-08 00:47:34 +0000 2020/12/08 00:47:34 aborting: could not write to output stream: Expected HTTP Status 200, received: "400 Bad Request"
2020-12-08 00:47:34 +0000 pg_restore: error: could not read from input file: end of file
2020-12-08 00:47:34 +0000 waiting for restore to complete
2020-12-08 00:47:34 +0000 pg_restore finished with errors
2020-12-08 00:47:34 +0000 waiting for download to complete
2020-12-08 00:47:34 +0000 download finished with errors
2020-12-08 00:47:34 +0000 please check the source URL and ensure it is publicly accessible

请有人指出我哪里出错了?

谢谢!

【问题讨论】:

  • 无论何时在命令行上使用预签名的 URL,都需要引用它们。它们包含诸如& 这样对shell 有意义的结构。因此,通常您应该将预签名的 URL 用双引号括起来。

标签: postgresql amazon-web-services amazon-s3 heroku


【解决方案1】:

为了调试它,从请求中获取响应错误消息到您的 AWS url 可能很有用,您可以使用 curl 轻松完成:

curl <SIGNED AWS URL>

就我而言,我收到以下消息:

Error parsing the X-Amz-Credential parameter; the region 'us-east-1' is wrong; expecting 'eu-west-3'

这意味着由于某种原因,AWS 使用错误的区域签署了我的 URL。如果我尝试手动将正确的区域添加到我的签名网址,则请求将失败,因为签名不匹配。

相反,我需要通过在 presign 命令中指定区域来让 AWS 正确地对 URL 进行签名:

aws s3 presign s3://URL --region eu-west-3

当我这样做并将生成的签名 url 与 heroku pg:backups:restore 命令一起使用时,请求成功并且heroku 能够导入我的数据库。

【讨论】:

    【解决方案2】:

    诀窍是将额外的引号传递给heroku 命令。

    它看起来像这样:

    • powershell:

      $url = "https://provider/bucket/file"
      heroku pg:backups:restore -a <your-app-name> "`"$url`""
      
    • 重击:

      url="https://provider/bucket/file"
      heroku pg:backups:restore -a <your-app-name> "$url"
      

    【讨论】:

    • 我用这种方法得到+0000 pg_restore: error: did not find magic string in file header
    猜你喜欢
    • 1970-01-01
    • 2021-12-28
    • 1970-01-01
    • 2014-09-01
    • 1970-01-01
    • 2018-12-27
    • 1970-01-01
    • 2020-07-27
    • 1970-01-01
    相关资源
    最近更新 更多