【问题标题】:Not able to upload file to aws s3 using shell script无法使用 shell 脚本将文件上传到 aws s3
【发布时间】:2015-12-21 01:01:38
【问题描述】:

我在尝试上传到 s3 时遇到以下错误。下面的脚本似乎是正确的,但我仍然遇到错误。请有人帮我解决这个错误。我的密钥和访问 ID 是正确的,因为我能够使用 java 和 ruby​​ 中的这些密钥连接到 AWS。

<?xml version="1.0" encoding="UTF-8"?>
<Error><Code>SignatureDoesNotMatch</Code><Message>The request signature we calculated does not match the signature you provided. Check your key and signing method.</Message><AWSAccessKeyId>AKIAJNODAIRHFUX3LHFQ</AWSAccessKeyId><StringToSign>PUT

application/x-compressed-tar
Sun, 20 Dec 2015 19:54:47 -0500
/test-pk-proj//home/rushi/pk.tar.gz</StringToSign><SignatureProvided>M1PcN+Umkq5WFtVVSerHRGNABb8=</SignatureProvided><StringToSignBytes>50 55 54 0a 0a 61 70 70 6c 69 63 61 74 69 6f 6e 2f 78 2d 63 6f 6d 70 72 65 73 73 65 64 2d 74 61 72 0a 53 75 6e 2c 20 32 30 20 44 65 63 20 32 30 31 35 20 31 39 3a 35 34 3a 34 37 20 2d 30 35 30 30 0a 2f 74 65 73 74 2d 70 6b 2d 70 72 6f 6a 2f 2f 68 6f 6d 65 2f 72 75 73 68 69 2f 70 6b 2e 74 61 72 2e 67 7a</StringToSignBytes><RequestId>5439C7C84533E7C6</RequestId><HostId>620896ul+wnRwCjWl1ZtNZQ5NEJMGl29FqESC3iJyvnWhYhOECLlPl0417RfF3eovKFb7ac2.amazonaws.com port 443: Connection timed out

下面是我用来将数据上传到 s3 的 shell 脚本

file=/home/rushi/1.pdf
bucket=xxxxxxxxxxxxxxxxxxx
resource="/${bucket}/${file}"
contentType="application/x-compressed-tar"
dateValue=`date -R`
stringToSign="PUT\n\n${contentType}\n${dateValue}\n${resource}"
s3Key=xxxxxxxxxxxxxxxxxxxxxxxxxxx
s3Secret=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
signature=`echo -en ${stringToSign} | openssl sha1 -hmac ${s3Secret} -binary | base64`
curl -L -X PUT -T "${file}" \
  -H "Host: ${bucket}.s3-website-us-west-2.amazonaws.com" \
  -H "Date: ${dateValue}" \
  -H "Content-Type: ${contentType}" \
  -H "Authorization: AWS ${s3Key}:${signature}" \
  https://${bucket}.s3-website-us-west-2.amazonaws.com/${file}

【问题讨论】:

  • 你不可能打算在bucket和key之间使用双重//,对吗?如果文件有一个前导斜杠,就像这里一样,那么resource="/${bucket}/${file}" 应该是resource="/${bucket}${file}",但不清楚这是您唯一的问题,还是其中一个问题。
  • 第二期,https://${bucket}.s3-website-us-west-2.amazonaws.com/${file} 错了,两次。同样,您将前导斜杠加倍,主机名应为bucket.s3-us-west-2.amazonaws.com - 您不能将上传直接发送到网站端点,无论如何它都不会说 https - 所以您的错误不会实际上对应于您显示的代码。

标签: shell amazon-s3 sh


【解决方案1】:

从下面给出的链接安装 AWS CLI

通过aws configure 命令配置AWS并输入密钥和区域

要将文件复制到 S3,请在 shell 脚本中使用此命令

aws s3 cp fileName s3://bucketName

链接:http://docs.aws.amazon.com/cli/latest/userguide/installing.html#install-bundle-other-os

p.s 如果您收到连接超时错误,请在安全组中打开端口 443 (HTTPS)。

【讨论】:

    【解决方案2】:

    请按照以下步骤,

    创建一个 IAM 用户,该用户有权在 s3 存储桶中上传文件。

    注意:您还可以创建一个策略,您可以在其中访问您所在地区的所有可用 s3 存储桶,您可以将该策略附加到您在 IAM 中创建的用户,以便在 S3 存储桶中上传文件

    记下您的 IAM 用户的访问权限和密钥。

    登录到您的服务器,例如 [Aws linux 服务器]

    使用以下命令

    aws 配置

    输入访问密钥 输入密钥 输入区域,例如,如果它是 mumbau 区域,请使用 ap-south-1 输入输出为“文本”

    之后,您就可以将文件从服务器上传到 s3 存储桶了。

    使用示例命令上传 s3 存储桶中的文件

    aws s3 cp 或 mv 文件名 s3://bucketName

    上面的aws cli可以用于脚本上传文件。

    【讨论】:

      猜你喜欢
      • 2013-04-14
      • 2021-04-16
      • 2020-02-01
      • 2019-10-31
      • 2016-09-29
      • 2012-09-08
      • 2013-02-02
      • 2019-03-04
      • 2020-04-29
      相关资源
      最近更新 更多