Skip to main content
Nordlys logo, a drawing of two gray mountains with green northern lights in the background 陈迪の自留地

Back to all posts

python operation s3

Published on by Chen Di · 1 min read

Table of Contents

Show more

配置访问凭证

~/.aws/credentials

[default]
aws_access_key_id = AKIATXBI6Zxxxxx
aws_secret_access_key = bh4Z5Xq9cM/U/7XZxxxx

pip install boto3

import boto3

bucket_name = 'prd-uz'
file_path = 'test.md'
s3_key = 'test.md' #上传到 S3 的根目录

# 创建 S3 客户端
s3 = boto3.client('s3',region_name='me-central-1')

# 上传文件
s3.upload_file(file_path, bucket_name, s3_key)
print("File uploaded successfully.")


# 生成预签名 URL
url = s3.generate_presigned_url(
    'get_object',
    Params={'Bucket': bucket_name, 'Key': s3_key},
    ExpiresIn=3600
)
print("Presigned URL:", url)