Example #1
0
class Client(object):

    cos = settings.TECENT_COS
    appid, secret_id, secret_key = cos['APP_ID'], cos['SECRET_ID'], cos['SECRET_KEY']
    region_info = 'tj'
    baseurl = u'http://blog-1251509264.costj.myqcloud.com/'

    def __init__(self):
        self.bucket = u'blog'
        self.client = CosClient(
            self.appid,
            self.secret_id,
            self.secret_key,
            region=self.region_info
        )

    def upload(self,filename):
        sep = os.sep
        #if sep in filename,meaning that the filename is a absolute file path.
        if sep in filename:
            filename = os.path.abspath(filename)
            #the cos path is the name of file.
            cos_path = os.path.basename(filename)
            local_path = filename
        else:
            cos_path = filename
            #get the absolute path of the file,if it is only a filename without any path .
            local_path = os.path.abspath(filename)
        try:
            #sometimes,it needed to be encoded to utf-8
            cos_path, local_path = unicode('/'+cos_path), unicode(local_path)
        except:
            cos_path = '/' + cos_path
        request = UploadFileRequest(self.bucket,cos_path,local_path)
        result = self.client.upload_file(request)
        if result[u'code'] == 0:
            return result['data']['source_url']
        else:
            return {'fail':result['message']}


    def del_file(self,filename):
        if os.path.isdir(filename):
            return None
        request = DelFileRequest(self.bucket,
                                 unicode('/'+filename))
        result = self.client.del_file(request)
        print result
        return result
Example #2
0
def delete_cos(file):
    appid = 10000  # 根据自己的情况填写
    secret_id = u'# 根据自己的情况填写'
    secret_key = u'# 根据自己的情况填写'
    region = '# 根据自己的情况填写'
    bucket = u'# 根据自己的情况填写'

    cos_client = CosClient(appid, secret_id, secret_key, region)
    cos_path = u'/' + file

    request = DelFileRequest(bucket, cos_path)

    start = time.time()
    resp = cos_client.del_file(request)
    # assert resp['code'] == 0
    end = time.time()
    if resp['code'] != 0:
        return 0
    elapsed = (end - start) * 1000.0
    print 'COS Delete File Time %0.3f ms' % elapsed
    return elapsed
Example #3
0
from qcloud_cos import UploadFileRequest
from qcloud_cos import CosClient
import sys

if __name__ == '__main__':
    if len(sys.argv) < 3:
        print "argv error"

    bucket = u'koprvhdix117'
    app_id = 10038234
    secret_id = u'AKIDkTQGMuCeJvtTTlqg911BfF393ghumqHp'
    secret_key = u'ZE1uBa6jfbsB0vVyfbWhw5JuZKPwaEwh'

    cos_client = CosClient(app_id, secret_id, secret_key)

    if sys.argv[1] == "upload":
        cos_path = unicode(sys.argv[2])
        local_path = unicode(sys.argv[3])

        request = UploadFileRequest(bucket, cos_path, local_path)
        upload_file_ret = cos_client.upload_file(request)
        print upload_file_ret
    elif sys.argv[1] == "delete":
        cos_path = unicode(sys.argv[2])

        request = DelFileRequest(bucket, cos_path)
        delete_file_ret = cos_client.del_file(request)
        print delete_file_ret
    else:
        print "argv error"
Example #4
0
def cos_demo():
    # 设置用户属性, 包括appid, secret_id和secret_key
    # 这些属性可以在cos控制台获取(https://console.qcloud.com/cos)
    appid = 100000  # 替换为用户的appid
    secret_id = u'xxxxxxxx'  # 替换为用户的secret_id
    secret_key = u'xxxxxxx'  # 替换为用户的secret_key
    cos_client = CosClient(appid, secret_id, secret_key)

    # 设置要操作的bucket
    bucket = u'mybucket'

    ############################################################################
    # 文件操作                                                                 #
    ############################################################################
    # 1. 上传文件(默认不覆盖)
    #    将本地的local_file_1.txt上传到bucket的根分区下,并命名为sample_file.txt
    #    默认不覆盖, 如果cos上文件存在,则会返回错误
    request = UploadFileRequest(bucket, u'/sample_file.txt',
                                u'local_file_1.txt')
    upload_file_ret = cos_client.upload_file(request)
    print 'upload file ret:', repr(upload_file_ret)

    # 2. 上传文件(覆盖文件)
    #    将本地的local_file_2.txt上传到bucket的根分区下,覆盖已上传的sample_file.txt
    request = UploadFileRequest(bucket, u'/sample_file.txt',
                                u'local_file_2.txt')
    request.set_insert_only(0)  # 设置允许覆盖
    upload_file_ret = cos_client.upload_file(request)
    print 'overwrite file ret:', repr(upload_file_ret)

    # 3. 获取文件属性
    request = StatFileRequest(bucket, u'/sample_file.txt')
    stat_file_ret = cos_client.stat_file(request)
    print 'stat file ret:', repr(stat_file_ret)

    # 4. 更新文件属性
    request = UpdateFileRequest(bucket, u'/sample_file.txt')

    request.set_biz_attr(u'这是个demo文件')  # 设置文件biz_attr属性
    request.set_authority(u'eWRPrivate')  # 设置文件的权限
    request.set_cache_control(u'cache_xxx')  # 设置Cache-Control
    request.set_content_type(u'application/text')  # 设置Content-Type
    request.set_content_disposition(u'ccccxxx.txt')  # 设置Content-Disposition
    request.set_content_language(u'english')  # 设置Content-Language
    request.set_x_cos_meta(u'x-cos-meta-xxx', u'xxx')  # 设置自定义的x-cos-meta-属性
    request.set_x_cos_meta(u'x-cos-meta-yyy', u'yyy')  # 设置自定义的x-cos-meta-属性

    update_file_ret = cos_client.update_file(request)
    print 'update file ret:', repr(update_file_ret)

    # 5. 更新后再次获取文件属性
    request = StatFileRequest(bucket, u'/sample_file.txt')
    stat_file_ret = cos_client.stat_file(request)
    print 'stat file ret:', repr(stat_file_ret)

    # 6. 移动文件, 将sample_file.txt移动位sample_file_move.txt
    request = MoveFileRequest(bucket, u'/sample_file.txt',
                              u'/sample_file_move.txt')
    stat_file_ret = cos_client.move_file(request)
    print 'move file ret:', repr(stat_file_ret)

    # 7. 删除文件
    request = DelFileRequest(bucket, u'/sample_file_move.txt')
    del_ret = cos_client.del_file(request)
    print 'del file ret:', repr(del_ret)

    ############################################################################
    # 目录操作                                                                 #
    ############################################################################
    # 1. 生成目录, 目录名为sample_folder
    request = CreateFolderRequest(bucket, u'/sample_folder/')
    create_folder_ret = cos_client.create_folder(request)
    print 'create folder ret:', create_folder_ret

    # 2. 更新目录的biz_attr属性
    request = UpdateFolderRequest(bucket, u'/sample_folder/', u'这是一个测试目录')
    update_folder_ret = cos_client.update_folder(request)
    print 'update folder ret:', repr(update_folder_ret)

    # 3. 获取目录属性
    request = StatFolderRequest(bucket, u'/sample_folder/')
    stat_folder_ret = cos_client.stat_folder(request)
    print 'stat folder ret:', repr(stat_folder_ret)

    # 4. list目录, 获取目录下的成员
    request = ListFolderRequest(bucket, u'/sample_folder/')
    list_folder_ret = cos_client.list_folder(request)
    print 'list folder ret:', repr(list_folder_ret)

    # 5. 删除目录
    request = DelFolderRequest(bucket, u'/sample_folder/')
    delete_folder_ret = cos_client.del_folder(request)
    print 'delete folder ret:', repr(delete_folder_ret)
Example #5
0
def cos_demo():
    # 设置用户属性, 包括appid, secret_id和secret_key
    # 这些属性可以在cos控制台获取(https://console.qcloud.com/cos)
    appid = 100000  # 替换为用户的appid
    secret_id = u"xxxxxxxx"  # 替换为用户的secret_id
    secret_key = u"xxxxxxx"  # 替换为用户的secret_key
    cos_client = CosClient(appid, secret_id, secret_key)

    # 设置要操作的bucket
    bucket = u"mybucket"

    ############################################################################
    # 文件操作                                                                 #
    ############################################################################
    # 1. 上传文件(默认不覆盖)
    #    将本地的local_file_1.txt上传到bucket的根分区下,并命名为sample_file.txt
    #    默认不覆盖, 如果cos上文件存在,则会返回错误
    request = UploadFileRequest(bucket, u"/sample_file.txt", u"local_file_1.txt")
    upload_file_ret = cos_client.upload_file(request)
    print "upload file ret:", repr(upload_file_ret)

    # 2. 上传文件(覆盖文件)
    #    将本地的local_file_2.txt上传到bucket的根分区下,覆盖已上传的sample_file.txt
    request = UploadFileRequest(bucket, u"/sample_file.txt", u"local_file_2.txt")
    request.set_insert_only(0)  # 设置允许覆盖
    upload_file_ret = cos_client.upload_file(request)
    print "overwrite file ret:", repr(upload_file_ret)

    # 3. 获取文件属性
    request = StatFileRequest(bucket, u"/sample_file.txt")
    stat_file_ret = cos_client.stat_file(request)
    print "stat file ret:", repr(stat_file_ret)

    # 4. 更新文件属性
    request = UpdateFileRequest(bucket, u"/sample_file.txt")

    request.set_biz_attr(u"这是个demo文件")  # 设置文件biz_attr属性
    request.set_authority(u"eWRPrivate")  # 设置文件的权限
    request.set_cache_control(u"cache_xxx")  # 设置Cache-Control
    request.set_content_type(u"application/text")  # 设置Content-Type
    request.set_content_disposition(u"ccccxxx.txt")  # 设置Content-Disposition
    request.set_content_language(u"english")  # 设置Content-Language
    request.set_x_cos_meta(u"x-cos-meta-xxx", u"xxx")  # 设置自定义的x-cos-meta-属性
    request.set_x_cos_meta(u"x-cos-meta-yyy", u"yyy")  # 设置自定义的x-cos-meta-属性

    update_file_ret = cos_client.update_file(request)
    print "update file ret:", repr(update_file_ret)

    # 5. 更新后再次获取文件属性
    request = StatFileRequest(bucket, u"/sample_file.txt")
    stat_file_ret = cos_client.stat_file(request)
    print "stat file ret:", repr(stat_file_ret)

    # 6. 移动文件, 将sample_file.txt移动位sample_file_move.txt
    request = MoveFileRequest(bucket, u"/sample_file.txt", u"/sample_file_move.txt")
    stat_file_ret = cos_client.move_file(request)
    print "move file ret:", repr(stat_file_ret)

    # 7. 删除文件
    request = DelFileRequest(bucket, u"/sample_file_move.txt")
    del_ret = cos_client.del_file(request)
    print "del file ret:", repr(del_ret)

    ############################################################################
    # 目录操作                                                                 #
    ############################################################################
    # 1. 生成目录, 目录名为sample_folder
    request = CreateFolderRequest(bucket, u"/sample_folder/")
    create_folder_ret = cos_client.create_folder(request)
    print "create folder ret:", create_folder_ret

    # 2. 更新目录的biz_attr属性
    request = UpdateFolderRequest(bucket, u"/sample_folder/", u"这是一个测试目录")
    update_folder_ret = cos_client.update_folder(request)
    print "update folder ret:", repr(update_folder_ret)

    # 3. 获取目录属性
    request = StatFolderRequest(bucket, u"/sample_folder/")
    stat_folder_ret = cos_client.stat_folder(request)
    print "stat folder ret:", repr(stat_folder_ret)

    # 4. list目录, 获取目录下的成员
    request = ListFolderRequest(bucket, u"/sample_folder/")
    list_folder_ret = cos_client.list_folder(request)
    print "list folder ret:", repr(list_folder_ret)

    # 5. 删除目录
    request = DelFolderRequest(bucket, u"/sample_folder/")
    delete_folder_ret = cos_client.del_folder(request)
    print "delete folder ret:", repr(delete_folder_ret)