我有第三方提供给我的s3链接,其结构如下:http://s3.amazonaws.com/bucket_name_possibly/path/to/file_possibly/filename?AWSAccessKeyId=SomeKey&Expires=888888&Signature=SomeCharactersPossiblyHTMLencoded
点击链接为我下载文件。但是,在python中,当我尝试在链接上使用urllib.request.urlretrieve(link_string)
时,我收到错误HTTP Error 403: Forbidden
我也尝试过使用boto3并手动解析出bucket_name,key,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
。
我被卡住了,我怎样才能让python以编程方式下载链接?
答案 0 :(得分:0)
您可以使用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
有关详细信息,请参阅this