async def put_object_from_file(self, key, filename, headers=None, progress_callback=None): """上传一个本地文件到OSS的普通文件。 :param str key: 上传到OSS的文件名 :param str filename: 本地文件名,需要有可读权限 :param headers: 用户指定的HTTP头部。可以指定Content-Type、Content-MD5、x-oss-meta-开头的头部等 :type headers: 可以是dict,建议是oss2.CaseInsensitiveDict :param progress_callback: 用户指定的进度回调函数。参考 :ref:`progress_callback` :return: :class:`PutObjectResult <oss2.models.PutObjectResult>` """ headers = utils.set_content_type(http.CaseInsensitiveDict(headers), filename) with open(to_unicode(filename), 'rb') as f: return await self.put_object(key, f, headers=headers, progress_callback=progress_callback)
def get_sts(self): clt = client.AcsClient(OSS_STS_ID, OSS_STS_KEY, OSS_REGION) req = AssumeRoleRequest.AssumeRoleRequest() req.set_accept_format('json') req.set_RoleArn(OSS_STS_ARN) req.set_RoleSessionName('oss-python-sdk-example') body = clt.do_action_with_exception(req) j = json.loads(compat.to_unicode(body)) return j['Credentials']['AccessKeyId'], j['Credentials'][ 'AccessKeySecret'], j['Credentials']['SecurityToken']
async def get_object_to_file(self, key, filename, byte_range=None, headers=None, progress_callback=None, process=None): """下载一个文件到本地文件。 :param key: 文件名 :param filename: 本地文件名。要求父目录已经存在,且有写权限。 :param byte_range: 指定下载范围。参见 :ref:`byte_range` :param headers: HTTP头部 :type headers: 可以是dict,建议是oss2.CaseInsensitiveDict :param progress_callback: 用户指定的进度回调函数。参考 :ref:`progress_callback` :param process: oss文件处理,如图像服务等。指定后process,返回的内容为处理后的文件。 :return: 如果文件不存在,则抛出 :class:`NoSuchKey <oss2.exceptions.NoSuchKey>` ;还可能抛出其他异常 """ with open(to_unicode(filename), 'wb') as f: result = await self.get_object(key, byte_range=byte_range, headers=headers, progress_callback=progress_callback, process=process) if result.content_length is None: shutil.copyfileobj(result, f) else: utils.copyfileobj_and_verify(result, f, result.content_length, request_id=result.request_id) return result
def corrupt_record(store, store_key, r): pathname = store._ResumableStoreBase__path(store_key) with open(to_unicode(pathname), 'w') as f: f.write('hello}')