s3的移植使用卷曲.pl到Python

2024-09-29 23:27:25 发布

您现在位置:Python中文网/ 问答频道 /正文

我一直在运行cmd命令:

~/s3curl/s3curl.pl --id mapreduce -- -sf https://$SERVER/$PATH >> $TEMP_FILE

我想把我的脚本移植到Python上。你知道吗

我试过:

import boto3
client = boto3.client('s3')
response = client.get_object(Bucket=<server>, Key=<path>)

但我有个错误:

botocore.exceptions.ClientError: An error occurred (AllAccessDisabled) when calling the GetObject operation: All access to this object has been disabled

我做错什么了?你知道吗

谢谢!你知道吗


Tags: pathhttps命令cmdclientidobjectserver
1条回答
网友
1楼 · 发布于 2024-09-29 23:27:25

原来有一个名为.s3curl的文件与s3curl.pl位于同一个目录中,其中包含一个用户id和加密密钥。你知道吗

我将其转换为一个名为s3.yaml的yaml文件,其中包含:

awsSecretAccessKeys:
  mapreduce:
    id: <insert id here>
    key: <insert key here>

而Python的解决方案是:

def download_file_from_s3(s3_server, path, export_path):
    url = s3_server + path
    with open('s3.yaml') as f:
        s3_conf = yaml.load(f.read())['awsSecretAccessKeys']['mapreduce']

    now = datetime.now().strftime('%a, %d %b %Y %H:%M:%S +0000')
    to_sign = 'GET\n\n\n{}\n{}'.format(now, path)
    signature = hmac.new(s3_conf['key'], to_sign, sha1).digest().encode("base64").rstrip('\n')
    response = requests.get(url, headers={'Date': now, 'Authorization': 'AWS {}:{}'.format(s3_conf['id'], signature)})

    response.raise_for_status()

    with open(export_path, 'ab') as f:
        for block in response.iter_content(4096):
            f.write(block)

相关问题 更多 >

    热门问题