![](/img/trans.png)
[英]How to upload file to folder in aws S3 bucket using python boto3
[英]How I will get response of success in aws file upload on s3 bucket using boto3?
我知道如何使用 boto3 將文件上傳到 s3 存儲桶上。 但是我已經使用了我的功能,我想檢查圖像是否已成功上傳到 s3 存儲桶,如果已上傳,則我想執行操作。
所以這里是這樣的例子,
import boto3
def upload_image_get_url(file_name, bucket, key_name):
s3 = boto3.client("s3")
result = s3.upload_file(file_name, bucket, key_name) # Here I got none so How I will check like file is upoaded or not?
if result == 'success' or result == True:
response = "https://{0}.s3.us-east-2.amCCazonaws.com/{1}".format(bucket, key_name)
else:
response = False
return response
所以我的要求是直截了當的,如果我成功上傳文件,那么我將返回 s3 url 作為響應。 所以請幫助我,你的幫助將不勝感激。
該upload_file()
函數沒有返回值。
如果上傳有問題,會拋出異常。
例如,如果找不到要上傳的本地文件,則會引發FileNotFoundError
異常。 (試一試!)
或者,您可以使用put_object()
返回一個字典:
import os.path
import boto3
def upload_image_get_url(file_name, bucket, key_name):
s3 = boto3.client("s3")
# Get the file name
name = os.path.basename(file_name)
# Format the key
s3_key = '{0}/{1}'.format(key_name, name)
# Send the file
with open(file_name, 'rb') as fd:
result = s3.put_object(
Bucket=bucket,
Key=s3_key,
Body=fd
)
if result['ResponseMetadata']['HTTPStatusCode'] == 200:
response = "https://{0}.s3.us-east-2.amCCazonaws.com/{1}".format(bucket, s3_key)
else:
response = False
return response
結果將包含這些鍵:
'ResponseMetadata':
'RequestId': '...'
'HostId': '...'
'HTTPStatusCode': 200
'HTTPHeaders':
'x-amz-id-2': '...'
'x-amz-request-id': '...'
'date': 'Fri, 15 Nov 2019 10:56:15 GMT'
'x-amz-version-id': '...'
'x-amz-server-side-encryption': 'AES256'
'etag': '"..."'
'content-length': '0'
'server': 'AmazonS3'
'RetryAttempts': 0
'ETag': '"..."'
'ServerSideEncryption': 'AES256'
'VersionId': '...'
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.