gpt4 book ai didi

python-3.6 - OCI (Oracle Cloud) 子隔间

转载 作者:行者123 更新时间:2023-12-04 01:05:34 26 4
gpt4 key购买 nike

问题:这是 oci (oracle cloud) 的 python 代码。它能够在 root compartment 的 bucket 中创建 Bucket 或下载和上传正常。但我无法在子隔间上做同样的事情。子隔间是“My_Sub_Compartment”

请告知如何修复它。

import os
import oci
import io
from oci.config import from_file

data_dir = "D:\\DataScienceAndStats\\artificialintelligence\\CS223A"
files_to_process = [file for file in os.listdir(data_dir) if file.endswith('txt')]
bucket_name = "Sales_Data"

# this is to configure the oci configuration file
my_config = from_file(file_location="C:\\Users\\amits\\Desktop\\Oracle_Cloud\\config_file_oci.txt")
print(my_config)

# Test Configuration file of oci
# print(validate_config(my_config))

"""
Create object storage client and get its namespace
"""
object_storage_client = oci.object_storage.ObjectStorageClient(my_config)
namespace = object_storage_client.get_namespace().data

"""
Create a bucket if it does not exist
"""
try:
create_bucket_response = object_storage_client.create_bucket(namespace,
oci.object_storage.models.CreateBucketDetails(name=bucket_name, compartment_id=my_config['tenancy']))

except Exception as e:
print("Please read below messages")
print(e.message)
print(e.status)

"""
Uploading the files
"""
print("uploading files to bucket")
for upload_file in files_to_process:
print('Uploading file {}'.format(upload_file))
object_storage_client.put_object(namespace, bucket_name, upload_file, io.open(os.path.join(data_dir, upload_file),
'rb'))

"""
Listing a files in the Bucket
"""

object_list = object_storage_client.list_objects(namespace, bucket_name)
for o in object_list.data.objects:
print(o.name)

"""
Downloading files from Bucket
"""
object_name = "1.txt"
destination_dir = 'D:\\DataScienceAndStats\\artificialintelligence\\CS223A\\moved_files'.format(object_name)
get_obj = object_storage_client.get_object(namespace, bucket_name, object_name)
with open(os.path.join(destination_dir, object_name), 'wb') as f:
for chunk in get_obj.data.raw.stream(1024 * 1024, decode_content=False):
f.write(chunk)

最佳答案

在 create_bucket_response 函数中,您应该提供子隔间 OCID,而不是 my_config['tenancy']。你试过这种方法吗? my_config['tenancy'] 将始终返回根分区。

关于python-3.6 - OCI (Oracle Cloud) 子隔间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54494397/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com