使用场景
需要在cos桶上传图片的同时对这个图片加水印(存储桶是私有读写,水印模板图片权限继承(私有读))
参考官网python sdk 本地路径 简单上传 demo,以及 图片水印 上传时处理 demo,如下代码:
# -*- coding=utf-8
from qcloud_cos import CosConfig
from qcloud_cos import CosS3Client
import sys,base64
import logging
logging.basicConfig(level=logging.INFO, stream=sys.stdout)
secret_id = '**************'
secret_key = '**************'
token = None
scheme = 'https'
region = 'ap-guangzhou'
bucket = '********'
config = CosConfig(Region=region, SecretId=secret_id, SecretKey=secret_key, Token=token, Scheme=scheme)
client = CosS3Client(config)
# 生成水印图片base64
watermark_url = 'http://{bucket}.cos.{region}.myqcloud.com/shuiyin1.jpg'.format(bucket=bucket, region=region)
watermark_url_base64 = bytes.decode(base64.b64encode(str.encode(watermark_url)))
# 本地路径 简单上传
response = client.put_object_from_local_file(
Bucket = bucket,
LocalFilePath = r'C:\tmp\2.jpeg',
Key = "admin.jpeg",
# pic operation json struct
PicOperations='{"is_pic_info":1,"rules":[{"fileid": "/2.jpeg","rule": "watermark/1/image/' + watermark_url_base64 + '" }]}')
备注图片处理参数:
is_pic_info:是否返回原图信息。0表示不返回原图信息,1表示返回原图信息,默认为0
rules:处理规则,一条规则对应一个处理结果(目前最多支持五条规则),不填则不进行图片处理
fileid:图片处理后文件的保存路径及名称
rule:处理参数
watermark:1 图片水印、2 文字水印、3 盲水印
image:水印图片地址,需要经过 URL 安全的 Base64 编码 处理。
报错
qcloud_cos.cos_exception.CosServiceError: {'code': 'NoSuchKey', 'message': 'The specified key does not exist.', 'resource': '/admin.jpeg', 'requestid': '***************', 'traceid': '*********************************************'}
原因:
1、水印模板图片是私有读,需要将水印图片带上签名访问
2、水印模板图片URL需要经过 URL 安全的 Base64 编码 处理,否则cos云端解码报错会找不到水印图片
以下完整code:
# -*- coding=utf-8
from qcloud_cos import CosConfig
from qcloud_cos import CosS3Client
import sys,base64
import logging
logging.basicConfig(level=logging.INFO, stream=sys.stdout)
secret_id = '**************'
secret_key = '**************'
token = None
scheme = 'https'
region = 'ap-guangzhou'
bucket = '********'
config = CosConfig(Region=region, SecretId=secret_id, SecretKey=secret_key, Token=token, Scheme=scheme)
client = CosS3Client(config)
# 给私有读水印图片生成预签名url
signed_url = client.get_presigned_url(
Method = 'GET',
Bucket = bucket,
Key = '/shuiyin1.jpg', # 云上私有读水印模板
Expired=3600 # 3600秒后过期
)
# 生成水印文件base64
watermark_url = 'http://{bucket}.cos.{region}.myqcloud.com/shuiyin1.jpg?{parameter}'.format(bucket=bucket, region=region,parameter=signed_url.split("?")[1])
watermark_url_base64 = bytes.decode(base64.b64encode(str.encode(watermark_url)))
# 转为URL安全的 BASE64 编码
url_base64 = watermark_url_base64.replace('+','-').replace('/','_').replace('=','')
# 本地路径 简单上传
response = client.put_object_from_local_file(
Bucket = bucket,
LocalFilePath = r'C:\tmp\2.jpeg',
Key = "admin.jpeg",
# pic operation json struct
PicOperations='{"is_pic_info":1,"rules":[{"fileid": "/2.jpeg","rule": "watermark/1/image/' + url_base64 + '" }]}')
print(response)
返回:
{'Content-Type': 'application/xml', 'Content-Length': '827', 'Connection': 'keep-alive', 'Date': 'Tue, 16 May 2023 03:39:35 GMT', 'ETag': '"60df2287de06b***************"', 'Server': 'tencent-cos', 'x-cos-hash-crc64ecma': '113437186**********', 'x-cos-request-id': 'NjQ2MmZhZjdfZD**********', 'x-cos-storage-class': 'STANDARD', 'x-cos-version-id': 'MTg0NDUwNTk4Nj**********'}
cos桶查看上传并加水印的图片
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。