45 lines
1.4 KiB
Python
45 lines
1.4 KiB
Python
import boto3
|
|
from botocore.exceptions import NoCredentialsError
|
|
|
|
# 使用访问密钥ID和秘密访问密钥来初始化S3客户端
|
|
aws_access_key_id = 'AKIAZQ3DT3KLN5WGXZOR' # 替换为你的访问密钥ID
|
|
aws_secret_access_key = '5UZb8SovTrbroT7yU1pBzaR5myLn+NMA+c87RvLH' # 替换为你的秘密访问密钥
|
|
region_name = 'ap-northeast-1' # 替换为你的S3存储桶所在区域
|
|
|
|
s3_client = boto3.client(
|
|
's3',
|
|
aws_access_key_id=aws_access_key_id,
|
|
aws_secret_access_key=aws_secret_access_key,
|
|
region_name=region_name
|
|
)
|
|
|
|
|
|
def generate_presigned_url(bucket_name, object_key, expiration=3600):
|
|
try:
|
|
response = s3_client.generate_presigned_url(
|
|
'put_object',
|
|
Params={
|
|
'Bucket': bucket_name,
|
|
'Key': object_key,
|
|
'ContentType': 'application/pdf' # 设置Content-Type
|
|
},
|
|
ExpiresIn=expiration
|
|
)
|
|
except NoCredentialsError:
|
|
print("Credentials not available")
|
|
return None
|
|
return response
|
|
|
|
|
|
# 生成每个学生的上传预签名URL
|
|
bucket_name = 'sure-ae-upload'
|
|
folder_name = 'sure_homework_define_by_qin'
|
|
students = ['alice', 'bob', 'charlie'] # 示例学生名单
|
|
|
|
for student in students:
|
|
object_key = f'{folder_name}/{student}-assignment.pdf' # 动态生成路径
|
|
url = generate_presigned_url(bucket_name, object_key)
|
|
|
|
if url:
|
|
print(f"The presigned URL for {student} is: {url}")
|