编辑
2024-08-16
即用脚本
00

目录

上传文件到s3桶

上传文件到s3桶

安装boto3 pip install boto3

python
import boto3 # pip install boto3 from botocore.exceptions import NoCredentialsError s3 = boto3.client( 's3', aws_access_key_id='AKIAZ*****SN63FG', aws_secret_access_key='LxDB6Rxy*****VZ97n9fo', region_name='**-****-*' ) local_file = "static/id_card_test5.png" bucket_name = "*****-*****" s3_file_name = "test/ssssssss5.png" def upload_to_aws(local_file, bucket, s3_file, isOpenAcl=False): try: s3.upload_file(local_file, bucket, s3_file) print("Upload Successful") # 是否打开公开访问 if isOpenAcl: s3.put_object_acl(ACL='public-read', Bucket=bucket, Key=s3_file) url = f"https://{bucket_name}.s3.amazonaws.com/{s3_file_name}" url = s3.generate_presigned_url( 'get_object', Params={'Bucket': bucket_name, 'Key': s3_file_name}, ExpiresIn=3600) return True except FileNotFoundError: print(f"The file {local_file} was not found") return False except NoCredentialsError: print("Credentials not available") return False uploaded = upload_to_aws(local_file, bucket_name, s3_file_name)

本文作者:Freed

本文链接:

版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!