0

我正在使用 OCI Python SDK,当我尝试下载 GZ 格式的对象(从 OCI 存储桶)时,它正在下载,但文件大小为零字节。附上代码任何帮助都是非常有用的。

import os
import oci
import io
import sys

reporting_namespace = 'xygabcdef'

prefix_file = "abc/xyz"

# Update these values
destination_path = 'downloaded_reports'

# Make a directory to receive reports
if not os.path.exists(destination_path):
    os.mkdir(destination_path)

# Get the list of reports
config = oci.config.from_file(oci.config.DEFAULT_LOCATION, oci.config.DEFAULT_PROFILE)
reporting_bucket = sys.argv[1]
object_storage = oci.object_storage.ObjectStorageClient(config)
report_bucket_objects = object_storage.list_objects(reporting_namespace, reporting_bucket, prefix=prefix_file)

#def download_audit():
for o in report_bucket_objects.data.objects:
    print('Found file ' + o.name)
    object_details = object_storage.get_object(reporting_namespace, reporting_bucket, o.name)
    print (object_details)
    filename = o.name.rsplit('/', 1)[-1]
    with open(destination_path + '/' + filename, 'wb') as f:
        for chunk in object_details.data.raw.stream(1024 * 1024, decode_content=False):
            f.write(chunk)

4

1 回答 1

0

请参阅此处的示例。这对你有用吗?即:

get_obj = object_storage.get_object(namespace, bucket_name, example_file_object_name)
with open('example_file_retrieved', 'wb') as f:
    for chunk in get_obj.data.raw.stream(1024 * 1024, decode_content=False):
        f.write(chunk)

在您的示例destintation_path中似乎未定义,并且似乎有错字(目的地-> 目的地)。这可能是问题吗?

最后,object_details 将文件大小/内容长度报告为什么?可能是对象存储中对象的文件大小本身为 0 字节。

于 2020-05-15T19:37:04.247 回答