繁体   English   中英

如何使用python从带有签名和有效期的链接中下载S3文件?

[英]How do I use python to download an S3 file from a link with a signature and expiration?

我有第三方提供的s3链接,其结构如下: http://s3.amazonaws.com/bucket_name_possibly/path/to/file_possibly/filename?AWSAccessKeyId=SomeKey&Expires=888888&Signature=SomeCharactersPossiblyHTMLencoded ://s3.amazonaws.com/bucket_name_possfully/path/to/file_possfully/filename?AWSAccessKeyId=SomeKey&Expires=888888&Signature=SomeCharactersPossibleHTMLencoded

单击链接为我下载文件。 但是,在python中,当我尝试在链接上使用urllib.request.urlretrieve(link_string)时,出现错误HTTP Error 403: Forbidden

我也尝试过使用boto3并手动解析出bucket_name,密钥,AWSAccessKeyID和签名(将其作为AWSSecretAccessKey进行处理-我知道这可能是错误的)。 我使用凭据设置了客户端,然后尝试运行get_object方法。 类似于以下内容:

client= boto3.client(
  's3',
  aws_access_key_id='AWSACCESSKEY',
  aws_secret_access_key='SomeCharactersPossiblyHTMLencoded',
  config=Config(signature_version='s3v4') # tried with/without this option
)
client.get_object(
   Bucket='bucket_name_possibly',
   Key='path/to/file_possibly/filename'
 )

产生的错误是An error occurred (SignatureDoesNotMatch) when calling the GetObject operation: The request signature we calculated does not match the signature you provided. Check your key and signing method An error occurred (SignatureDoesNotMatch) when calling the GetObject operation: The request signature we calculated does not match the signature you provided. Check your key and signing method

我被卡住了,如何获取python以编程方式下载链接?

您可以使用boto如下下载文件。

import boto3
import botocore

BUCKET_NAME = 'my-bucket' # replace with your bucket name
KEY = 'my_image_in_s3.jpg' # replace with your object key

s3 = boto3.resource('s3')

try:
    s3.Bucket(BUCKET_NAME).download_file(KEY, 'my_local_image.jpg')
except botocore.exceptions.ClientError as e:
    if e.response['Error']['Code'] == "404":
        print("The object does not exist.")
    else:
        raise

有关更多信息,您可以参考

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM