def delete_bucket(bucketname): try: res = oss.delete_bucket(bucketname) if (res.status / 100) == 2: logging.info('delete bucket %s successfully' % bucketname) else: logging.info('delete bucket %s failed' % bucketname) raise S3OperateError('delete bucket %s failed' % bucketname, None) except: logging.info( 'delete bucket %s failed,beacuase of the connection interrupt' % (bucketname)) raise S3OperateError('delete bucket %s failed' % bucketname, 'beacuase of the connection interrupt')
def create_bucket(bucketname): acl = '' headers = {} try: res = oss.create_bucket(bucketname, acl, headers) if (res.status / 100) == 2: logging.info('create bucket %s successfully' % (bucketname)) else: logging.info('create bucket %s failed' % (bucketname)) raise S3OperateError('create bucket %s failed' % bucketname, None) except: logging.info( 'create bucket %s failed,beacuase of the connection interrupt' % (bucketname)) raise S3OperateError('create bucket %s failed' % bucketname, 'beacuase of the connection interrupt')
def delete_all_file(bucketname): objs = list_all_file(bucketname) if objs: for obj in objs: delete(bucketname, obj) else: logging.info('bucket %s have no files ' % bucketname) raise S3OperateError('bucket %s have no files' % bucketname, None)
def put_common(local_file, put_method): if os.path.exists(local_file): bucket_name = get_bucketname(local_file) s3_relative_path = local_file[(len(settings.FILESTORE_DIR) + 1):] res = put_method(bucket_name, s3_relative_path, local_file) else: logging.info('local file %s not exists,can not put to s3' % local_file) raise S3OperateError( 'local file %s not exists,can not put to s3' % local_file, None)
def list_all_file(bucketname): ao = GetAllObjects() try: ao.get_all_object_in_bucket(oss, bucketname, "", "") logging.info('get files from s3 bucket %s successfully' % bucketname) except: raise S3OperateError( 'get files from s3 bucket %s failed' % bucketname, None) return ao.object_list
def putByDeliveryType(local_file, delivery_type): if os.path.exists(local_file): bucket_name = s3utils.getBucketNameByDeliveryType(delivery_type) s3_relative_path = local_file[(len(settings.FILESTORE_DIR) + 1):] put_file(bucket_name, s3_relative_path, local_file) return urlparse.urljoin(s3utils.getUrlByDeliveryType(delivery_type), s3_relative_path) else: logging.info('local file %s not exists,can not put to s3' % local_file) raise S3OperateError( 'local file %s not exists,can not put to s3' % local_file, None)
def delete(bucketname, s3path): flag = False headers = {} res1 = None try: res1 = oss.get_object(bucketname, s3path, headers) except: logging.info( 'get file %s in s3 bucket %s failed ,beacuase of the connection interrupt' % (s3path, bucketname)) if (res1.status / 100) == 2: res = None try: res = oss.delete_object(bucketname, s3path, headers) if (res.status / 100) == 2: logging.info('delete file %s from s3 bucket %s successfully' % (s3path, bucketname)) flag = True else: logging.info('delete file %s from s3 bucket %s failed' % (s3path, bucketname)) raise S3OperateError( 'delete file %s from s3 bucket %s failed' % (s3path, bucketname), None) except: logging.info( 'delete file %s from s3 bucket %s failed,beacuase of the connection interrupt' % (s3path, bucketname)) raise S3OperateError( 'delete file %s from s3 bucket %s failed' % (s3path, bucketname), 'beacuase of the connection interrupt') else: logging.info('file %s not exists on s3 bucket %s' % (s3path, bucketname)) raise S3OperateError( 'file %s not exists on s3 bucket %s' % (s3path, bucketname), None) return flag
def getVideo(video_id, local_file, get_method): kernel_db = dbutils.KERNEL_DB video = list( kernel_db.select('Video', where="id = $id ", vars={'id': video_id})) if video: logging.info('videoid: %s ,get video from s3 base on videoid' % video_id) s3_path = video[0].videoKey local_path = os.path.join(settings.FILESTORE_DIR, s3_path) if os.path.exists(local_path): copy_file(local_path, local_file) else: get_file(s3_path, get_method, settings.S3_VOD_BUCKET) else: logging.info('videoid: %s , video not exists,can get from s3' % video_id) raise S3OperateError( 'videoid: %s , video not exists,can get from s3' % video_id, None)