无法通过boto3下载S3存储桶对象。错误403 HeadObject:禁止

时间:2020-01-06 16:33:50

标签: python amazon-web-services amazon-s3 boto3

我知道这里还有其他有关此问题的话题,但仍在努力寻找正确的解决方案。我正在尝试使用以下python脚本在S3存储桶(我确实有权访问)中下载一组特定对象。运行脚本时,第一个对象成功下载,但随后引发此错误(403):

botocore.exceptions.ClientError: An error occurred (403) when calling the HeadObject operation: Forbidden

请参见下面的代码:

import csv
import boto3
import re
import logging
from botocore.exceptions import ClientError

prod_number_array_bq = []
prod_number_array_s3 = []
with open('bq-results-20191218-151637-rshujisvqrri.csv') as csv_file:
    csv_reader = csv.reader(csv_file,delimiter=',')
    line_count = 0
    for row in csv_reader:
        sliced = re.sub("[^0-9]", "", str(row))
        prod_number_array_bq.append(sliced)

s3 = boto3.resource('s3')
bucket = s3.Bucket('********')

for key in bucket.objects.all():
    sliced = re.sub("[^0-9]", "", str(key.key))
    if((set(sliced) & set(prod_number_array_bq))!=""):
            bucket.download_file(key.key,sliced + '.txt')

我们将不胜感激:)

谢谢

1 个答案:

答案 0 :(得分:0)

就我而言,我可以读取文件但无法下载

所以我下面将打印文件信息

resp = s3_client.list_objects_v2(Bucket=bucket_name, Prefix=origin)
print(resp)

但是这会给botocore.exceptions.ClientError: An error occurred (403) when calling the HeadObject operation: Forbidden错误

s3_client.download_file(bucket_name, origin, destination) 

问题是模型是从其他AWS账户上传的。我们在上传时缺少ACL。所以,

所以我们使用以下命令上传了文件

s3_client.upload_file(origin,
                      bucket_name,
                      destination,
                      ExtraArgs={'ACL':'bucket-owner-full-control'})

这导致我们按预期读取和下载文件。