python-3.x - 使用 Python boto3 在 AWS 中上传图像
问题描述
我尝试在 AWS s3-Bucket 中上传图像,我正在使用 boto3 并且可以正常工作,但是当我打开链接时,它会自动下载图像,并且当我手动上传图像时会在 web-browser 中打开。什么是问题?
这是我的代码。
import boto3
from botocore.exceptions import NoCredentialsError
ACCESS_KEY = 'xxxxxxxxxxxxxxxxx'
SECRET_KEY = 'xxxxxxxxxxxxxxxxx+4Tr4nUp'
def upload_to_aws(local_file, bucket, s3_file):
s3 = boto3.client('s3', aws_access_key_id=ACCESS_KEY,
aws_secret_access_key=SECRET_KEY)
try:
s3.upload_file(local_file, bucket, s3_file)
print("Upload Successful")
return True
except FileNotFoundError:
print("The file was not found")
return False
except NoCredentialsError:
print("Credentials not available")
return False
uploaded = upload_to_aws('test.jpg', 'testverifcation', 'test.jpg')
解决方案
try:
s3.upload_file(local_file, bucket, s3_file,ExtraArgs={'ACL': 'public-read', 'ContentType': 'image'})
print("Upload Successful")
return True
except FileNotFoundError:
print("The file was not found")
return False
except NoCredentialsError:
print("Credentials not available")
return False
推荐阅读
- java - 在 Azure 搜索上上传文档后,我可以查询它吗?
- pact - 如何处理 Pact 上的多态端点?
- r - 如何修复 R 中的“找不到对象”错误
- qt - 如何在 Qt 5.12 中使用 /std:c++latest
- linux - 为什么我的 Raspberry Pi SPI 写入循环会暂停大约 15 毫秒?上下文切换?
- html - 如何修复这个第二层下拉菜单背景?
- routing - Angular6:将数据从导航头传递到路由组件
- javascript - 如何在角度 onInit 方法中测试订阅功能?
- angular - ngClass 不影响任何东西
- javascript - 如何模拟'mousemove'事件并在TypeScript中指定movementX和movementY