@@ -2043,6 +2043,151 @@ def append_object(self, Bucket, Key, Position, Data, **kwargs):
2043
2043
response = rt .headers
2044
2044
return response
2045
2045
2046
+ def put_object_from_local_file (self , Bucket , LocalFilePath , Key , EnableMD5 = False , ** kwargs ):
2047
+ """本地文件上传接口,适用于小文件,最大不得超过5GB
2048
+
2049
+ :param Bucket(string): 存储桶名称.
2050
+ :param LocalFilePath(string): 上传文件的本地路径.
2051
+ :param Key(string): COS路径.
2052
+ :param EnableMD5(bool): 是否需要SDK计算Content-MD5,打开此开关会增加上传耗时.
2053
+ :kwargs(dict): 设置上传的headers.
2054
+ :return(dict): 上传成功返回的结果,包含ETag等信息.
2055
+
2056
+ .. code-block:: python
2057
+
2058
+ config = CosConfig(Region=region, Secret_id=secret_id, Secret_key=secret_key, Token=token) # 获取配置对象
2059
+ client = CosS3Client(config)
2060
+ # 上传本地文件到cos
2061
+ response = client.put_object(
2062
+ Bucket='bucket',
2063
+ LocalFilePath='local.txt',
2064
+ Key='test.txt'
2065
+ )
2066
+ print response['ETag']
2067
+ """
2068
+ with open (LocalFilePath , 'rb' ) as fp :
2069
+ return self .put_object (Bucket , fp , Key , EnableMD5 , ** kwargs )
2070
+
2071
+ def object_exists (self , Bucket , Key ):
2072
+ """判断一个文件是否存在
2073
+
2074
+ :param Bucket(string): 存储桶名称.
2075
+ :param Key(string): COS路径.
2076
+ :return(bool): 文件是否存在,返回True为存在,返回False为不存在
2077
+
2078
+ .. code-block:: python
2079
+
2080
+ config = CosConfig(Region=region, Secret_id=secret_id, Secret_key=secret_key, Token=token) # 获取配置对象
2081
+ client = CosS3Client(config)
2082
+ # 上传本地文件到cos
2083
+ status = client.object_exists(
2084
+ Bucket='bucket',
2085
+ Key='test.txt'
2086
+ )
2087
+ """
2088
+ try :
2089
+ self .head_object (Bucket , Key )
2090
+ return True
2091
+ except CosServiceError as e :
2092
+ if e .get_status_code () == 404 :
2093
+ return False
2094
+ else :
2095
+ raise e
2096
+
2097
+ def bucket_exists (self , Bucket ):
2098
+ """判断一个存储桶是否存在
2099
+
2100
+ :param Bucket(string): 存储桶名称.
2101
+ :return(bool): 存储桶
2102
+ 是否存在,返回True为存在,返回False为不存在.
2103
+
2104
+ .. code-block:: python
2105
+
2106
+ config = CosConfig(Region=region, Secret_id=secret_id, Secret_key=secret_key, Token=token) # 获取配置对象
2107
+ client = CosS3Client(config)
2108
+ # 上传本地文件到cos
2109
+ status = client.bucket_exists(
2110
+ Bucket='bucket'
2111
+ )
2112
+ """
2113
+ try :
2114
+ self .head_bucket (Bucket )
2115
+ return True
2116
+ except CosServiceError as e :
2117
+ if e .get_status_code () == 404 :
2118
+ return False
2119
+ else :
2120
+ raise e
2121
+
2122
+ def change_object_storage_class (self , Bucket , Key , StorageClass ):
2123
+ """改变文件的存储类型
2124
+
2125
+ :param Bucket(string): 存储桶名称.
2126
+ :param Key(string): COS路径.
2127
+ :param StorageClass(bool): 是否需要SDK计算Content-MD5,打开此开关会增加上传耗时.
2128
+ :kwargs(dict): 设置上传的headers.
2129
+ :return(dict): 上传成功返回的结果,包含ETag等信息.
2130
+
2131
+ .. code-block:: python
2132
+
2133
+ config = CosConfig(Region=region, Secret_id=secret_id, Secret_key=secret_key, Token=token) # 获取配置对象
2134
+ client = CosS3Client(config)
2135
+ # 上传本地文件到cos
2136
+ response = client.change_object_storage_class(
2137
+ Bucket='bucket',
2138
+ Key='test.txt',
2139
+ StorageClass='STANDARD'
2140
+ )
2141
+ """
2142
+ copy_source = {
2143
+ 'Bucket' : Bucket ,
2144
+ 'Key' : Key ,
2145
+ 'Region' : self ._conf ._region ,
2146
+ 'Appid' : self ._conf ._appid
2147
+ }
2148
+ response = self .copy_object (
2149
+ Bucket = Bucket ,
2150
+ Key = Key ,
2151
+ CopySource = copy_source ,
2152
+ CopyStatus = 'Replaced' ,
2153
+ StorageClass = StorageClass
2154
+ )
2155
+ return response
2156
+
2157
+ def update_object_meta (self , Bucket , Key , ** kwargs ):
2158
+ """改变文件的存储类型
2159
+
2160
+ :param Bucket(string): 存储桶名称.
2161
+ :param Key(string): COS路径.
2162
+ :kwargs(dict): 设置文件的元属性.
2163
+ :return: None.
2164
+
2165
+ .. code-block:: python
2166
+
2167
+ config = CosConfig(Region=region, Secret_id=secret_id, Secret_key=secret_key, Token=token) # 获取配置对象
2168
+ client = CosS3Client(config)
2169
+ # 上传本地文件到cos
2170
+ response = client.update_object_meta(
2171
+ Bucket='bucket',
2172
+ Key='test.txt',
2173
+ ContentType='text/html'
2174
+ )
2175
+ """
2176
+ copy_source = {
2177
+ 'Bucket' : Bucket ,
2178
+ 'Key' : Key ,
2179
+ 'Region' : self ._conf ._region ,
2180
+ 'Appid' : self ._conf ._appid
2181
+ }
2182
+ response = self .copy_object (
2183
+ Bucket = Bucket ,
2184
+ Key = Key ,
2185
+ CopySource = copy_source ,
2186
+ CopyStatus = 'Replaced' ,
2187
+ ** kwargs
2188
+ )
2189
+ return response
2190
+
2046
2191
2047
2192
if __name__ == "__main__" :
2048
2193
pass
0 commit comments