使用 Boto 下载 S3 文件
Download S3 Files with Boto
我正在尝试设置一个应用程序,用户可以在其中下载存储在 S3 存储桶中的文件。我能够设置我的存储桶,并获得正确的文件,但它不会下载,给我这个错误:No such file or directory: 'media/user_1/imageName.jpg'
知道为什么吗?这似乎是一个相对容易的问题,但我似乎不太明白。我可以正确删除一张图片,所以它能够识别正确的图片。
这是我的 views.py
def download(request, project_id=None):
conn = S3Connection('AWS_BUCKET_KEY', 'AWS_SECRET_KEY')
b = Bucket(conn, 'BUCKET_NAME')
k = Key(b)
instance = get_object_or_404(Project, id=project_id)
k.key = 'media/'+str(instance.image)
k.get_contents_to_filename(str(k.key))
return redirect("/dashboard/")
问题是您正在下载到 不存在的本地目录 (media/user1
)。您需要:
- 先在本机创建目录
- 只需使用文件名而不是完整路径
- 使用完整路径,但将斜杠 (
/
) 替换为另一个字符 -- 这将确保文件名的唯一性而无需创建目录
最后一个选项可以通过以下方式实现:
k.get_contents_to_filename(str(k.key).replace('/', '_'))
另请参阅:
使用 boto3 下载文件非常简单,在使用此代码之前在系统级别配置您的 AWS 凭证。
client = boto3.client('s3')
// if your bucket name is mybucket and the file path is test/abc.txt
// then the Bucket='mybucket' Prefix='test'
resp = client.list_objects_v2(Bucket="<your bucket name>", Prefix="<prefix of the s3 folder>")
for obj in resp['Contents']:
key = obj['Key']
//to read s3 file contents as String
response = client.get_object(Bucket="<your bucket name>",
Key=key)
print(response['Body'].read().decode('utf-8'))
//to download the file to local
client.download_file('<your bucket name>', key, key.replace('test',''))
replace是在你本地找到s3文件名的文件,如果不替换会尝试另存为'test/abc.txt'.
import os
import boto3
import json
s3 = boto3.resource('s3', aws_access_key_id="AKIAxxxxxxxxxxxxJWB",
aws_secret_access_key="LV0+vsaxxxxxxxxxxxxxxxxxxxxxry0/LjxZkN")
my_bucket = s3.Bucket('s3testing')
# download file into current directory
for s3_object in my_bucket.objects.all():
# Need to split s3_object.key into path and file name, else it will give error file not found.
path, filename = os.path.split(s3_object.key)
my_bucket.download_file(s3_object.key, filename)
我正在尝试设置一个应用程序,用户可以在其中下载存储在 S3 存储桶中的文件。我能够设置我的存储桶,并获得正确的文件,但它不会下载,给我这个错误:No such file or directory: 'media/user_1/imageName.jpg'
知道为什么吗?这似乎是一个相对容易的问题,但我似乎不太明白。我可以正确删除一张图片,所以它能够识别正确的图片。
这是我的 views.py
def download(request, project_id=None):
conn = S3Connection('AWS_BUCKET_KEY', 'AWS_SECRET_KEY')
b = Bucket(conn, 'BUCKET_NAME')
k = Key(b)
instance = get_object_or_404(Project, id=project_id)
k.key = 'media/'+str(instance.image)
k.get_contents_to_filename(str(k.key))
return redirect("/dashboard/")
问题是您正在下载到 不存在的本地目录 (media/user1
)。您需要:
- 先在本机创建目录
- 只需使用文件名而不是完整路径
- 使用完整路径,但将斜杠 (
/
) 替换为另一个字符 -- 这将确保文件名的唯一性而无需创建目录
最后一个选项可以通过以下方式实现:
k.get_contents_to_filename(str(k.key).replace('/', '_'))
另请参阅:
使用 boto3 下载文件非常简单,在使用此代码之前在系统级别配置您的 AWS 凭证。
client = boto3.client('s3')
// if your bucket name is mybucket and the file path is test/abc.txt
// then the Bucket='mybucket' Prefix='test'
resp = client.list_objects_v2(Bucket="<your bucket name>", Prefix="<prefix of the s3 folder>")
for obj in resp['Contents']:
key = obj['Key']
//to read s3 file contents as String
response = client.get_object(Bucket="<your bucket name>",
Key=key)
print(response['Body'].read().decode('utf-8'))
//to download the file to local
client.download_file('<your bucket name>', key, key.replace('test',''))
replace是在你本地找到s3文件名的文件,如果不替换会尝试另存为'test/abc.txt'.
import os
import boto3
import json
s3 = boto3.resource('s3', aws_access_key_id="AKIAxxxxxxxxxxxxJWB",
aws_secret_access_key="LV0+vsaxxxxxxxxxxxxxxxxxxxxxry0/LjxZkN")
my_bucket = s3.Bucket('s3testing')
# download file into current directory
for s3_object in my_bucket.objects.all():
# Need to split s3_object.key into path and file name, else it will give error file not found.
path, filename = os.path.split(s3_object.key)
my_bucket.download_file(s3_object.key, filename)