CodeSample小助手 2019-12-30
对象标签使用一组键值对(Key-Value)来标记对象。对象标签的详情请参考开发指南的对象标签。
+‑=._:/
# -*- coding: utf-8 -*-
import oss2
from oss2.headers import OSS_OBJECT_TAGGING
# 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录https://ram.console.aliyun.com创建RAM账号。
auth = oss2.Auth('<yourAccessKeyId>', '<yourAccessKeySecret>')
# Endpoint以杭州为例,其它Region请按实际情况填写。
bucket = oss2.Bucket(auth, 'http://oss-cn-hangzhou.aliyuncs.com', '<yourBucketName>')
object_name = '<yourObjectName>'
# http header中设置标签信息。
headers = dict()
headers[OSS_OBJECT_TAGGING] = "k1=v1&k2=v2&k3=v3"
# put_object接口指定headers,将会给上传的Object添加标签。
result = bucket.put_object(object_name, 'content', headers=headers)
print('http response status: ', result.status)
# 查看Object的标签信息。
result = bucket.get_object_tagging('yourObjectName')
for key in result.tag_set.tagging_rule:
print('tagging key: {}, value: {}'.format(key, result.tag_set.tagging_rule[key]))
# -*- coding: utf-8 -*-
import os
import oss2
from oss2 import SizedFileAdapter, determine_part_size
from oss2.models import PartInfo
from oss2.headers import OSS_OBJECT_TAGGING
# 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录https://ram.console.aliyun.com创建RAM账号。
auth = oss2.Auth('<yourAccessKeyId>', '<yourAccessKeySecret>')
# Endpoint以杭州为例,其它Region请按实际情况填写。
bucket = oss2.Bucket(auth, 'http://oss-cn-hangzhou.aliyuncs.com', '<yourBucketName>')
object_name = '<yourObjectName>'
filename = '<yourLocalFile>'
total_size = os.path.getsize(filename)
# determine_part_size方法用来确定分片大小。
part_size = determine_part_size(total_size, preferred_size=100 * 1024)
# http header中设置标签信息。
headers = dict()
headers[OSS_OBJECT_TAGGING] = 'k1=v1&k2=v2'
# 初始化分片。
# init_multipart_upload接口指定headers,将会给上传的文件添加标签。
upload_id = bucket.init_multipart_upload(object_name, headers=headers).upload_id
parts = []
# 逐个上传分片。
with open(filename, 'rb') as fileobj:
part_number = 1
offset = 0
while offset < total_size:
num_to_upload = min(part_size, total_size - offset)
# SizedFileAdapter(fileobj, size)方法会生成一个新的文件对象,重新计算起始追加位置。
result = bucket.upload_part(object_name, upload_id, part_number,
SizedFileAdapter(fileobj, num_to_upload))
parts.append(PartInfo(part_number, result.etag))
offset += num_to_upload
part_number += 1
# 完成分片上传。
result = bucket.complete_multipart_upload(object_name, upload_id, parts)
print('http response status: ', result.status)
# 查看文件的的标签信息。
result = bucket.get_object_tagging(object_name)
for key in result.tag_set.tagging_rule:
print('tagging key: {}, value: {}'.format(key, result.tag_set.tagging_rule[key]))
# 验证分片上传。
with open(filename, 'rb') as fileobj:
assert bucket.get_object(object_name).read() == fileobj.read()
# -*- coding: utf-8 -*-
import oss2
from oss2.headers import OSS_OBJECT_TAGGING
# 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录https://ram.console.aliyun.com创建RAM账号。
auth = oss2.Auth('<yourAccessKeyId>', '<yourAccessKeySecret>')
# Endpoint以杭州为例,其它Region请按实际情况填写。
bucket = oss2.Bucket(auth, 'http://oss-cn-hangzhou.aliyuncs.com', '<yourBucketName>')
object_name = '<yourObjectName>'
# http header中设置标签信息。
headers = dict()
headers[OSS_OBJECT_TAGGING] = "k1=v1&k2=v2&k3=v3"
# 追加上传文件, append_object接口指定header,将会给文件设置标签。
# 只有第一次调用append_object设置的标签才会生效,后续再调用append_object设置的标签不生效。
result = bucket.append_object(object_name, 0, '<yourContent>', headers=headers)
# 查看object的标签信息。
result = bucket.get_object_tagging('yourObjectName')
for key in result.tag_set.tagging_rule:
print('tagging key: {}, value: {}'.format(key, result.tag_set.tagging_rule[key]))
# -*- coding: utf-8 -*-
import oss2
from oss2.headers import OSS_OBJECT_TAGGING
# 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录https://ram.console.aliyun.com创建RAM账号。
auth = oss2.Auth('<yourAccessKeyId>', '<yourAccessKeySecret>')
# Endpoint以杭州为例,其它Region请按实际情况填写。
bucket = oss2.Bucket(auth, 'http://oss-cn-hangzhou.aliyuncs.com', '<yourBucketName>')
object_name = '<yourObjectName>'
local_file = '<yourLocalFileName>'
# http header中设置标签信息。
headers = dict()
headers[OSS_OBJECT_TAGGING] = "k1=v1&k2=v2&k3=v3"
# 当文件长度大于或等于可选参数multipart_threshold(默认值为10MB)时,会使用分片上传。如未使用参数store指定目录,则会在HOME目录下建立.py-oss-upload目录来保存断点信息。
# resumable_upload接口指定headers,将会给上传的文件添加标签。
oss2.resumable_upload(bucket, object_name, local_file, headers=headers)
result = bucket.get_object_tagging(object_name)
for key in result.tag_set.tagging_rule:
print('object tagging key: {}, value: {}'.format(key, result.tag_set.tagging_rule[key]))
# -*- coding: utf-8 -*-
import oss2
from oss2.models import Tagging, TaggingRule
# 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录https://ram.console.aliyun.com创建RAM账号。
auth = oss2.Auth('<yourAccessKeyId>', '<yourAccessKeySecret>')
# Endpoint以杭州为例,其它Region请按实际情况填写。
bucket = oss2.Bucket(auth, 'http://oss-cn-hangzhou.aliyuncs.com', '<yourBucketName>')
# 创建标签规则。
rule = TaggingRule()
rule.add('key1', 'value1')
rule.add('key2', 'value2')
# 创建标签。
tagging = Tagging(rule)
# 设置标签。
result = bucket.put_object_tagging('<yourObjectName>', tagging)
# 查看HTTP返回码。
print('http response status:', result.status)
以下分别提供了简单拷贝1GB以下的Object、及分片拷贝1GB以上的Object时设置对象标签的详细示例。
# -*- coding: utf-8 -*-
import oss2
from oss2.headers import OSS_OBJECT_TAGGING, OSS_OBJECT_TAGGING_COPY_DIRECTIVE
# 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录https://ram.console.aliyun.com创建RAM账号。
auth = oss2.Auth('<yourAccessKeyId>', '<yourAccessKeySecret>')
# Endpoint以杭州为例,其它Region请按实际情况填写。
bucket = oss2.Bucket(auth, 'http://oss-cn-hangzhou.aliyuncs.com', '<yourBucketName>')
# 源文件。
src_object_name = '<yourtSrcObjectName>'
# 目标文件1。
dest_object_name1 = '<yourtDestObjectName1>'
# 目标文件2。
dest_object_name2 = '<yourtDestObjectName2>'
# http header中指定OSS_OBJECT_TAGGING_COPY_DIRECTIVE参数为COPY或者默认不指定,则目标文件1将会拥有源文件同样的标签信息。
headers=dict()
headers[OSS_OBJECT_TAGGING_COPY_DIRECTIVE] = 'COPY'
bucket.copy_object(bucket.bucket_name, src_object_name, dest_object_name1, headers=headers)
# http header中指定OSS_OBJECT_TAGGING_COPY_DIRECTIVE参数为REPLACE,则目标文件2将会设置为headers[OSS_OBJECT_TAGGING]指定的标签信息。
headers[OSS_OBJECT_TAGGING_COPY_DIRECTIVE] = 'REPLACE'
headers[OSS_OBJECT_TAGGING] = "key4=value4&key5=value5"
bucket.copy_object(bucket.bucket_name, src_object_name, dest_object_name2, headers=headers)
# 查看src_object_name的标签信息。
result = bucket.get_object_tagging(src_object_name)
for key in result.tag_set.tagging_rule:
print('src tagging key: {}, value: {}'.format(key, result.tag_set.tagging_rule[key]))
# 查看目标文件1的标签信息,将会跟源文件的标签信息保持一致。
result = bucket.get_object_tagging(dest_object_name1)
for key in result.tag_set.tagging_rule:
print('dest1 object tagging key: {}, value: {}'.format(key, result.tag_set.tagging_rule[key]))
# 查看目标文件2的标签信息,将会为headers[OSS_OBJECT_TAGGING]指定的标签信息。
result = bucket.get_object_tagging(dest_object_name2)
for key in result.tag_set.tagging_rule:
print('dest2 object tagging key: {}, value: {}'.format(key, result.tag_set.tagging_rule[key]))
# -*- coding: utf-8 -*-
import os
import oss2
from oss2 import determine_part_size
from oss2.models import PartInfo
from oss2.headers import OSS_OBJECT_TAGGING
# 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录https://ram.console.aliyun.com创建RAM账号。
auth = oss2.Auth('<yourAccessKeyId>', '<yourAccessKeySecret>')
# Endpoint以杭州为例,其它Region请按实际情况填写。
bucket = oss2.Bucket(auth, 'http://oss-cn-hangzhou.aliyuncs.com', '<yourBucketName>')
# 源文件。
src_object_name = '<yourSrcObject>'
# 目标文件。
dest_object_name = '<yourDestObject>'
# 获取源文件的文件大小。
head_info = bucket.head_object(src_object_name)
total_size = head_info.content_length
print('src object size:', total_size)
# determine_part_size方法用来确定分片大小。
part_size = determine_part_size(total_size, preferred_size=100 * 1024)
print('part_size:', part_size)
# http header中设置标签信息。
headers = dict()
headers[OSS_OBJECT_TAGGING] = 'k3=v3'
# 初始化分片。
# init_multipart_upload接口指定headers,将会给目标文件添加标签。
upload_id = bucket.init_multipart_upload(dest_object_name, headers=headers).upload_id
parts = []
# 逐个上传分片。
part_number = 1
offset = 0
while offset < total_size:
num_to_upload = min(part_size, total_size - offset)
end = offset + num_to_upload - 1;
result = bucket.upload_part_copy(bucket.bucket_name, src_object_name, (offset, end), dest_object_name, upload_id, part_number)
#保存part信息。
parts.append(PartInfo(part_number, result.etag))
offset += num_to_upload
part_number += 1
# 完成分片上传。
result = bucket.complete_multipart_upload(dest_object_name, upload_id, parts)
# 获取文件元信息。
head_info = bucket.head_object(dest_object_name)
# 查看目标文件大小。
dest_object_size = head_info.content_length
print('dest object size:', dest_object_size)
# 对比源文件大小。
assert dest_object_size == total_size
# 查看源文件的标签信息。
result = bucket.get_object_tagging(src_object_name)
for key in result.tag_set.tagging_rule:
print('src tagging key: {}, value: {}'.format(key, result.tag_set.tagging_rule[key]))
# 查看目标文件的标签信息。
result = bucket.get_object_tagging(dest_object_name)
for key in result.tag_set.tagging_rule:
print('dest tagging key: {}, value: {}'.format(key, result.tag_set.tagging_rule[key]))
# -*- coding: utf-8 -*-
import oss2
from oss2.headers import OSS_OBJECT_TAGGING
# 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录https://ram.console.aliyun.com创建RAM账号。
auth = oss2.Auth('<yourAccessKeyId>', '<yourAccessKeySecret>')
# Endpoint以杭州为例,其它Region请按实际情况填写。
bucket = oss2.Bucket(auth, 'http://oss-cn-hangzhou.aliyuncs.com', '<yourBucketName>')
object_name = '<yourObjectName>'
symlink_name = '<yourSymlinkName>'
# http header中设置标签信息。
headers = dict()
headers[OSS_OBJECT_TAGGING] = "k1=v1&k2=v2&k3=v3"
#添加软链接。
# put_symlink接口指定headers,将会给软链接文件添加标签。
result = bucket.put_symlink(object_name, symlink_name, headers=headers)
print('http response status: ', result.status)
# 查看软链接文件的标签信息。
result = bucket.get_object_tagging(symlink_name)
for key in result.tag_set.tagging_rule:
print('tagging key: {}, value: {}'.format(key, result.tag_set.tagging_rule[key]))