我有存储桶 URL、名称和文件列表对象键,需要下载文件。
我尝试了什么:
import boto3
import botocore
BUCKET_NAME = 'my-bucket'
KEY = 'my_file'
s3 = boto3.resource('s3')
try:
s3.Bucket(BUCKET_NAME).download_file(KEY, 'my_local_file')
except botocore.exceptions.ClientError as e:
if e.response['Error']['Code'] == "404":
print("The object does not exist.")
else:
raise
并发现错误:
Traceback (most recent call last):
File "D:\me\work\Mackpaw Data Engeneering test tsk\main.py", line 10, in <module>
s3.Bucket(BUCKET_NAME).download_file(KEY, 'data.json')
File "C:\Users\Katherine\AppData\Local\Programs\Python\Python39\lib\site-packages\boto3\s3\inject.py", line 244, in bucket_download_file
return self.meta.client.download_file(
File "C:\Users\Katherine\AppData\Local\Programs\Python\Python39\lib\site-packages\boto3\s3\inject.py", line 170, in download_file
return transfer.download_file(
File "C:\Users\Katherine\AppData\Local\Programs\Python\Python39\lib\site-packages\boto3\s3\transfer.py", line 307, in download_file
future.result()
File "C:\Users\Katherine\AppData\Local\Programs\Python\Python39\lib\site-packages\s3transfer\futures.py", line 106, in result
return self._coordinator.result()
File "C:\Users\Katherine\AppData\Local\Programs\Python\Python39\lib\site-packages\s3transfer\futures.py", line 265, in result
raise self._exception
File "C:\Users\Katherine\AppData\Local\Programs\Python\Python39\lib\site-packages\s3transfer\tasks.py", line 255, in _main
self._submit(transfer_future=transfer_future, **kwargs)
File "C:\Users\Katherine\AppData\Local\Programs\Python\Python39\lib\site-packages\s3transfer\download.py", line 340, in _submit
response = client.head_object(
File "C:\Users\Katherine\AppData\Local\Programs\Python\Python39\lib\site-packages\botocore\client.py", line 357, in _api_call
return self._make_api_call(operation_name, kwargs)
File "C:\Users\Katherine\AppData\Local\Programs\Python\Python39\lib\site-packages\botocore\client.py", line 662, in _make_api_call
http, parsed_response = self._make_request(
File "C:\Users\Katherine\AppData\Local\Programs\Python\Python39\lib\site-packages\botocore\client.py", line 682, in _make_request
return self._endpoint.make_request(operation_model, request_dict)
File "C:\Users\Katherine\AppData\Local\Programs\Python\Python39\lib\site-packages\botocore\endpoint.py", line 102, in make_request
return self._send_request(request_dict, operation_model)
File "C:\Users\Katherine\AppData\Local\Programs\Python\Python39\lib\site-packages\botocore\endpoint.py", line 132, in _send_request
request = self.create_request(request_dict, operation_model)
File "C:\Users\Katherine\AppData\Local\Programs\Python\Python39\lib\site-packages\botocore\endpoint.py", line 115, in create_request
self._event_emitter.emit(event_name, request=request,
File "C:\Users\Katherine\AppData\Local\Programs\Python\Python39\lib\site-packages\botocore\hooks.py", line 356, in emit
return self._emitter.emit(aliased_event_name, **kwargs)
File "C:\Users\Katherine\AppData\Local\Programs\Python\Python39\lib\site-packages\botocore\hooks.py", line 228, in emit
return self._emit(event_name, kwargs)
File "C:\Users\Katherine\AppData\Local\Programs\Python\Python39\lib\site-packages\botocore\hooks.py", line 211, in _emit
response = handler(**kwargs)
File "C:\Users\Katherine\AppData\Local\Programs\Python\Python39\lib\site-packages\botocore\signers.py", line 90, in handler
return self.sign(operation_name, request)
File "C:\Users\Katherine\AppData\Local\Programs\Python\Python39\lib\site-packages\botocore\signers.py", line 162, in sign
auth.add_auth(request)
File "C:\Users\Katherine\AppData\Local\Programs\Python\Python39\lib\site-packages\botocore\auth.py", line 373, in add_auth
raise NoCredentialsError()
botocore.exceptions.NoCredentialsError: Unable to locate credentials
似乎我正在尝试下载文件,就好像存储桶是私有的一样,它需要一些配置。但是这个存储桶是公开的,我还没有找到在这种情况下如何访问文件的信息。
答案 0 :(得分:1)
如果存储桶及其对象是公开的,您可以只使用对象的公开 URL 来检索所需的对象。公开网址格式如下:
https://
答案 1 :(得分:0)
我猜您还没有设置 aws 凭据 - 秘密和访问密钥。您可以按照以下链接进行操作:
https://boto3.amazonaws.com/v1/documentation/api/latest/guide/credentials.html