我可以使用wget或curl从私有存储桶下载文件吗?我只有访问权限和密钥凭据

ve7v8dk2  于 2022-11-13  发布在  其他
关注(0)|答案(2)|浏览(140)

我有一个私人桶,有一个单一的zip文件,需要下载它,但我不能使用aws cli或aws cmd。我可以用wget或curl吗?

mrfwxfqh

mrfwxfqh1#

是的,理论上您可以使用Amazon S3 REST API发出HTTP请求,但要使其正常工作,您需要正确地进行身份验证,例如,您可以使用身份验证头来完成此操作,然后您必须将其传递给curlwget
问题是,您需要编写代码来创建那些需要在Authentication Header中提供的有效签名,因此这可能会有点麻烦。

d8tt03nd

d8tt03nd2#

如果你把它命名为download.sh,假设你有一个名为my-bucket的bucket,其中有一个名为file.zip的文件,并且设置了aws关键环境变量,那么你应该能够通过调用来下载文件。
./download.sh file.zip my-bucket
我从一个类似的脚本中改编了这个脚本,以上传一个文件,我找到了here

#!/usr/bin/env bash

# Download a file from s3 without any 3rd party tools
# thanks https://www.gyanblog.com/aws/how-upload-aws-s3-curl/

file_path=$1
bucket=$2
set -eu pipefail
# about the file
filepath="/${bucket}/${file_path}"

# metadata
contentType="application/octet-stream"
dateValue=`date -R`
signature_string="GET\n\n${contentType}\n${dateValue}\n${filepath}"

#s3 keys
s3_access_key=$AWS_ACCESS_KEY_ID
s3_secret_key=$AWS_SECRET_ACCESS_KEY

#prepare signature hash to be sent in Authorization header
signature_hash=`echo -en ${signature_string} | openssl sha1 -hmac ${s3_secret_key} -binary | base64`

# actual curl command to do PUT operation on s3
curl -sSo ${file_path} \
  -H "Host: ${bucket}.s3.amazonaws.com" \
  -H "Date: ${dateValue}" \
  -H "Content-Type: ${contentType}" \
  -H "Authorization: AWS ${s3_access_key}:${signature_hash}" \
  https://${bucket}.s3.amazonaws.com/${file_path}

相关问题