使用CloudPathLib从公有AWS S3 Bucket下载文件夹遇凭证报错
公有AWS S3 Bucket下载文件时遭遇NoCredentialsError问题
我尝试使用Python库cloudpathlib从公有AWS S3 Bucket下载文件夹,代码如下:
from cloudpathlib import CloudPath path = r"C:\some\path\to\folder" url = "s3://some-example-bucket/folder/" cloud_path = CloudPath(url) cloud_path.download_to(path)
操作逻辑十分简单,按常理这个Bucket是公有的,代码应该能正常运行。
以下是Bucket策略(取自AWS S3教程):
{ "Version": "2012-10-17", "Statement": [ { "Sid": "PublicRead", "Effect": "Allow", "Principal": "*", "Action": [ "s3:GetObject", "s3:GetObjectVersion" ], "Resource": "arn:aws:s3:::cozy-auto-texture-sd-repo/*" } ] }
但运行代码下载stable-diffusion-v1-4文件夹时,出现如下报错:
File "E:\Cozy-Auto-Texture-Files\venv\lib\site-packages\botocore\auth.py", line 418, in add_auth raise NoCredentialsError() botocore.exceptions.NoCredentialsError: Unable to locate credentials
我的问题是:为何会出现这种情况?该Bucket是公有的,不应需要凭证。是Bucket权限配置存在遗漏,还是Python代码的问题?
补充说明:
我还尝试了使用boto3的方法:
BUCKET_NAME = 'cozy-auto-texture-sd-repo' # replace with your bucket name KEY = 'stable-diffusion-v1-4' s3 = boto3.resource('s3') try: s3.Bucket(BUCKET_NAME).download_file(KEY, sd_path) except botocore.exceptions.ClientError as e: if e.response['Error']['Code'] == "404": print("The object does not exist.") else: raise
但仍出现相同的NoCredentialsError报错,因此我怀疑是S3 Bucket的配置存在问题。
内容的提问来源于stack exchange,提问作者Torrin worx
相关产品推荐
相关产品推荐

