使用 S3 SDK 把 AWS SDK 与 mc 兼容客户端连到网关。
本教程将 AWS CLI 与 Python SDK(boto3)连接到 Silo PostgreSQL 网关,生成预签名下载 URL,并为浏览器跨域请求配置桶 CORS 规则。请先完成快速上手 ,准备好 attachments 桶、app-s3 用户与本地 hello.txt 文件。
在终端中设置网关地址、工作目录与受信任的 CA 证书路径:
export SILO_ENDPOINT = 'https://s3.example.net:9000'
export SILO_TUTORIAL_DIR = " $HOME /.local/share/silo-tutorial/files"
export AWS_CA_BUNDLE = '/path/to/s3-ca.crt' AWS CLI 与 Python SDK 使用 app-s3 凭据、地区 us-east-1 和路径风格寻址配置 AWS CLI profile(silo-app):
aws configure --profile silo-app
aws configure set s3.addressing_style path --profile silo-app
aws --profile silo-app --endpoint-url " $SILO_ENDPOINT " --region us-east-1 s3api list-objects-v2 --bucket attachments
aws --profile silo-app --endpoint-url " $SILO_ENDPOINT " --region us-east-1 s3 cp " $SILO_TUTORIAL_DIR /hello.txt" s3://attachments/hello.txt 创建 Python 3.10+ 虚拟环境并安装 boto3:
python3 -m venv " $HOME /.local/share/silo-tutorial/venv"
" $HOME /.local/share/silo-tutorial/venv/bin/pip" install 'boto3==1.43.108' 'botocore==1.43.108' 将以下脚本保存为 ~/.local/share/silo-tutorial/sdk.py:
import os
import boto3
from botocore.config import Config
session = boto3 . Session ( profile_name = "silo-app" , region_name = "us-east-1" )
s3 = session . client (
"s3" ,
endpoint_url = os . environ [ "SILO_ENDPOINT" ],
verify = os . environ [ "AWS_CA_BUNDLE" ],
config = Config ( signature_version = "s3v4" , s3 = { "addressing_style" : "path" }),
)
s3 . put_object ( Bucket = "attachments" , Key = "sdk.txt" , Body = b "hello from Python \n " )
response = s3 . get_object ( Bucket = "attachments" , Key = "sdk.txt" )
try :
print ( response [ "Body" ] . read () . decode ( "utf-8" ))
finally :
response [ "Body" ] . close () 运行 "$HOME/.local/share/silo-tutorial/venv/bin/python" "$HOME/.local/share/silo-tutorial/sdk.py",输出为 hello from Python。
请保持 SDK 默认的校验和计算与 TLS 证书验证处于开启状态。使用 SDK 分片上传助手上传较大文件时,合并后的对象总大小仍须满足存储上限(默认 256 MiB,初始化时最高可设为 1 GiB)。
预签名 URL 与桶 CORS 使用上文的 s3 客户端生成有效期为 15 分钟(900 秒)的预签名下载链接:
url = s3 . generate_presigned_url (
"get_object" , Params = { "Bucket" : "attachments" , "Key" : "sdk.txt" }, ExpiresIn = 900
)
print ( url ) 预签名 URL 沿用签名账号自身的权限,无法授予签名者原本没有的权限。若使用临时 STS 凭据签名,当底层会话先于 ExpiresIn 到期时,链接也会随之失效。
如需允许浏览器从其他源直接上传或下载对象,可使用管理 profile(silo-admin)配置桶 CORS 规则:
aws configure --profile silo-admin
aws configure set s3.addressing_style path --profile silo-admin
aws --profile silo-admin --endpoint-url " $SILO_ENDPOINT " --region us-east-1 s3api put-bucket-cors \
--bucket attachments \
--cors-configuration '{"CORSRules":[{"AllowedOrigins":["https://app.example.net"],"AllowedMethods":["GET","PUT","HEAD"],"AllowedHeaders":["*"],"ExposeHeaders":["ETag"],"MaxAgeSeconds":600}]}'
curl --cacert " $AWS_CA_BUNDLE " -i -X OPTIONS " $SILO_ENDPOINT /attachments/cors-demo.txt" \
-H 'Origin: https://app.example.net' \
-H 'Access-Control-Request-Method: PUT' \
-H 'Access-Control-Request-Headers: content-type' 匹配的预检请求会在响应头中返回允许的 Origin 和方法;被拒绝的预检请求返回 HTTP 403(AccessForbidden)。CORS 仅控制浏览器跨域响应头,不会绕过 SigV4 签名或 IAM 授权检查。
扫码打开此页 https://mimus.pgsty.com/zh/docs/s3/tutorial/sdk/
复制链接