Example #1
0
def file_upload_bos(config, local_filename):
    try:
        bucket_name = config['bucket_name']
        bucket_name = str(bucket_name)

        #p = config['dir']
        AK = config['AK']
        SK = config['SK']
        HOST = config['HOST']

        #print('step 1')
        #print('file name:',local_filename)
        bos_config = BceClientConfiguration(credentials=BceCredentials(
            str(AK), str(SK)),
                                            endpoint=str(HOST))
        bos_client = BosClient(bos_config)

        #print('step 2')
        #print 'bucket_name:',bucket_name,type(bucket_name)
        if not bos_client.does_bucket_exist(bucket_name):
            bos_client.create_bucket(bucket_name)

        #print('step 3')
        key = str(local_filename.split('/')[-1])
        #print('key:',key)
        bos_client.put_object_from_file(bucket_name, str(key),
                                        str(local_filename))
        print(local_filename + ' has been uploaded to bucket:' + bucket_name)
        return 0
    except Exception as e:
        print('file_upload_bos exception:' + str(e))
        traceback.print_exc()
        return -1
Example #2
0
class BOSStorage(BcsStorage):
    bucket_name = BCS_BUCKET['BUCKET_NAME']
    """
    这是一个支持BOS和本地django的FileStorage基类
    修改存储文件的路径和基本url
    """
    def __init__(self,
                 location=settings.MEDIA_URL,
                 base_url=settings.MEDIA_URL):
        super(BOSStorage, self).__init__(location, base_url)
        config = BceClientConfiguration(credentials=BceCredentials(
            BCS_BUCKET['AK'], BCS_BUCKET['SK']),
                                        endpoint=BCS_BUCKET['END_POINT'])
        self.bos_client = BosClient(config)
        # check if bucket exists
        if not self.bos_client.does_bucket_exist(self.bucket_name):
            self.bos_client.create_bucket(self.bucket_name)

    def saveToBucket(self, name, content):
        data = None
        if hasattr(content, '_get_file'):  # admin entry
            data = content._get_file().read()
        elif isinstance(content, (ContentFile)):  # view entry(ContentFile)
            data = content.read()
        else:
            data = content
        md5 = hashlib.md5()
        md5.update(data)
        md5value = base64.standard_b64encode(md5.digest())
        self.bos_client.put_object(self.bucket_name, name, data, len(data),
                                   md5value)

    def delete(self, name):
        """
        Delete a file from bos.
        """
        self.bos_client.delete_object(self.bucket_name, name)
    bucket_name = 'samplebucket-' + _random_string(6)
    key = 'samplekey' + _random_string(6)
    file_name = 'samplefile'
    download = 'download'

    ######################################################################################################
    #            bucket operation samples
    ######################################################################################################

    # create a bos client
    bos_client = BosClient(bos_sample_conf.config)

    # check if bucket exists
    if not bos_client.does_bucket_exist(bucket_name):
        bos_client.create_bucket(bucket_name)

    # delete a bucket(you can't  delete a bucket which is not empty)
    # clear it first
    for obj in bos_client.list_all_objects(bucket_name):
        bos_client.delete_object(bucket_name, obj.key)
    bos_client.delete_bucket(bucket_name)

    # create the bucket again
    bos_client.create_bucket(bucket_name)

    # list your buckets
    response = bos_client.list_buckets()
    for bucket in response.buckets:
        __logger.debug("[Sample] list buckets:%s", bucket.name)
Example #4
0
def getClient():
    bos_client = BosClient(bos_sample_conf.config)
    if not bos_client.does_bucket_exist(bucket_name):
        bos_client.create_bucket(bucket_name)
    return bos_client