从EC2实例下载AWS S3文件 [英] Download AWS S3 file from EC2 instance

查看:1070
本文介绍了从EC2实例下载AWS S3文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个脚本可以从AWS S3下载文件,并且可以在AWS之外的单独服务器上工作.但是,当我将此脚本放在EC2实例上并尝试使用它时,它将返回错误"SignatureDoesNotMatch-我们计算出的请求签名与您提供的签名不匹配.请检查您的密钥和签名方法."

I've a script to download file from AWS S3 and it works from a separate server outside of AWS. But when I put this script on an EC2 instance and tried it then it returns error "SignatureDoesNotMatch - The request signature we calculated does not match the signature you provided. Check your key and signing method."

但是它可以在另一台服务器上使用. ec2与s3主机位于同一区域.我猜它与主机有关.我尝试了这些主机/URL,但它返回了相同的错误.

But it works on the other server. The ec2 is on the same region as the s3 host. I'm guessing it has something to do with the host. I tried these host/url but it returns the same error.

任何人都可以通过curl从ec2下载s3文件吗?我只需要使用卷发.如果您知道如何卷曲,请回答.谢谢.

Anyone able to download s3 files from ec2 with curl? I need to use only curl. Please answer if you know how to do it through curl. Thanks.

https://s3-ap-southeast-1.amazonaws.com/ $ bucket/$ file

https://s3-ap-southeast-1.amazonaws.com/$bucket/$file

https://s3.amazonaws.com/ $ bucket/$ file

https://s3.amazonaws.com/$bucket/$file

https://$bucket.s3-ap-southeast-1.amazonaws.com/$file

https://$bucket.s3-ap-southeast-1.amazonaws.com/$file

https://$bucket.s3.amazonaws.com/$file

https://$bucket.s3.amazonaws.com/$file

#!/bin/sh
file="file-name"
bucket="bucket-name"
resource="/${bucket}/${file}"
contentType="application/x-compressed-tar"
dateValue="`date +'%a, %d %b %Y %H:%M:%S %z'`"
stringToSign="GET\n\n${contentType}\n${dateValue}\n${resource}"
s3Key="xxxxxxxxxxxxxxxxxx"
s3Secret="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
signature=$(echo -en "${stringToSign}" | openssl sha1 -hmac "${s3Secret}" -binary | base64)
curl -H "Host: s3-ap-southeast-1.amazonaws.com" \
 -H "Date: $dateValue" \
 -H "Content-Type: $contentType" \
 -H "Authorization: AWS ${s3Key}:${signature}" \
 https://s3-ap-southeast-1.amazonaws.com/$bucket/$file -o $file

推荐答案

与其编写CURL命令,不如考虑使用

Rather than writing a CURL command, consider using the AWS Command-Line Interface (CLI).

它具有aws s3 cp命令,该命令可以将内容复制到Amazon S3存储桶或从Amazon S3存储桶复制内容(甚至在存储桶之间).

It has a aws s3 cp command that can copy content to/from Amazon S3 buckets (and even between buckets).

这篇关于从EC2实例下载AWS S3文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆