对象存储访问有三种形式:1.基于自定义域名;2.对象存储厂商域名;3.空间名+对象存储厂商域名;下面使用的是第二种。
使用 AWS S3协议来访问对象存储空间中的文件,最后生成可以在浏览器中直接访问的链接,其它的方法类似:
# 首先要安装 boto3库
# pip install bots3
import boto3
from botocore.client import Config
# Config
s3endpoint = 'https://s3.cn-east-1.qiniucs.com' # bucket所在地区的域名
s3region = 'cn-east-1' # bucket所在的区域
s3accessKeyId = 'your ak'
s3SecretKeyId = 'your sk'
# Use S3 client
s3client = boto3.client(
's3',
aws_access_key_id = s3accessKeyId,
aws_secret_access_key = s3SecretKeyId,
endpoint_url = s3endpoint,
region_name = s3region,
config = Config(signature_version = "s3v4")
)
url = s3client.generate_presigned_url(
ClientMethod = "get_object",
Params = {
"Bucket" : "", # 空间名
"Key" : "" # 你的文件路径
}
)
print(url) # 打印文件的访问链接