Example #1
0
def delete_from_oss(endpoint, remote_path):
    bucket = get_bucket(endpoint)
    print("Deleting {0}...".format(remote_path))
    bucket.delete_object(remote_path)
    remote_files = _list_files(bucket, remote_path)
    if remote_files:
        bucket.batch_delete_objects(remote_files)
Example #2
0
def upload_to_oss(local_path, endpoint, remote_path):
    bucket = get_bucket(endpoint)
    local_path = os.path.abspath(local_path)
    if os.path.isdir(local_path):
        for top, dirs, files in os.walk(local_path):
            for file in files:
                local_file = os.path.join(top, file)
                remote_file = local_file.replace(local_path, remote_path)
                print("Uploading {0}...".format(local_file))
                oss2.resumable_upload(bucket, remote_file, local_file)
    else:
        print("Uploading {0}...".format(local_path))
        oss2.resumable_upload(bucket, remote_path, local_path)
Example #3
0
def copy_file(endpoint, src, dst):
    bucket = get_bucket(endpoint)
    print("Copying {0} to {1}...".format(src, dst))
    try:
        bucket.copy_object(bucket.bucket_name, src, dst)
    except (NotFound, NoSuchKey):
        if dst.startswith(src if src.endswith("/") else src + "/"):
            raise Exception("不能把文件夹复制到自己的子文件夹内")
        # FIXME: 只能复制 1000 个文件

        remote_files = _list_files(bucket, src, MAX_FILE_COUNT)
        for src_file in remote_files:
            dst_file = src_file.replace(src, dst)
            print("Copying {0}...".format(src_file))
            bucket.copy_object(bucket.bucket_name, src_file, dst_file)
Example #4
0
def download_from_oss(local_path, endpoint, remote_path):
    bucket = get_bucket(endpoint)
    print("Downloading {0}...".format(remote_path))
    try:
        return resumable_download(bucket, remote_path, local_path)
    except (NotFound, NoSuchKey):
        # FIXME: 最多只能下载 1000 个文件
        remote_files = _list_files(bucket, remote_path, MAX_FILE_COUNT)
        for remote_file in remote_files:
            rel = os.path.relpath(remote_file, remote_path)
            local_file = os.path.join(local_path, rel)
            local_file_dir = os.path.dirname(local_file)
            os.makedirs(local_file_dir, exist_ok=True)
            print("Downloading {0}...".format(remote_file))
            resumable_download(bucket, remote_file, local_file)
Example #5
0
def list_files(endpoint, remote_path, max_count=100):
    bucket = get_bucket(endpoint)
    return _list_files(bucket, remote_path, max_count)