我的程序是这样的,class CosStore(): """ 腾讯云第三方存储 """ def init(self): c = C.config.get('cos', None) # 获取配置文件,腾讯云COS配置 # c = C.config['cos'] secretId = c['secretId'] secretKey = c['secretKey'] region = c['region'] # 服务器地区,广州 token = None scheme = 'https' config = CosConfig(Region=region, SecretId=secretId, SecretKey=secretKey, Token=token, Scheme=scheme) self.client = CosS3Client(config) self.bucket = c['bucket'] # 腾讯云COS,配置文件的,根目录,bucket: live-banner-prod-1303153810 self.uri = c['uri'] # 腾讯云COS,配置文件的,uri print('store文件-51行_init_函数 :', self.client) __signle = None @classmethod def build(cls): if not cls.__signle: cls.__signle = CosStore() print('store文件-59行-build函数 init :', cls.__signle) return cls.__signle def save(self, body, path): response = self.client.put_object( Bucket=self.bucket, # bucket=COS根目录 Body=body, # body=zip二进制文件 Key=path, # 云的 path=文件目录全路径 # StorageClass='STANDARD', # 需要一个bytes类型的对象,而不是'str' EnableMD5=False) url = self.uri + path print(f"store文件-第85行: url={url}, etag={response['ETag']}") return url 我是这样调用的 with open(r'D:\python\python_objkt\project\dbpick-banner-master\server\data\store\batch\100.zip', mode='rb') as f: # zip文件全路径 body = f.read() path = 'upload/20230608/3c2dda611576b5b8.zip' url = CosStore.build().save(body, path) 运行报错报错qcloud_cos.cos_exception.CosClientError: a bytes-like object is required, not 'str'
时间: 2024-03-17 21:44:38 浏览: 91
Python项目开发实战:影视作品分析小程序,案例教程编程实例课程详解.pdf
这个错误提示指出参数应该是 bytes 类型的对象,而不是 str 类型。你可以将打开文件的方式改为二进制模式,即将 `mode='r'` 改为 `mode='rb'`。这样就可以将文件以二进制的方式读取了。你可以将打开文件的部分修改为:
```
with open(r'D:\python\python_objkt\project\dbpick-banner-master\server\data\store\batch\100.zip', mode='rb') as f:
body = f.read()
path = 'upload/20230608/3c2dda611576b5b8.zip'
url = CosStore.build().save(body, path)
```
阅读全文