Tutorial for accessing bucket through Python MCS SDK
In a multichain storage system, a bucket is a logical container that stores folders or files. Each bucket has a unique name and can contain any number of folders and files. Folders are virtual containers that organize files and help manage data, and files are individual data objects that can be of any type or format. MCS Bucket files are referred to as objects.
This section describes how to use the python-MCS-SDK to perform common operations on the MCS bucket.
from swan_mcs import APIClient, BucketAPImcs_api =APIClient("<API_KEY>", "<ACCESS_TOKEN>", "<CHAIN_NAME>")for i in bucket_client.list_buckets():print(i.to_json())
from swan_mcs import APIClient, BucketAPImcs_api =APIClient("<API_KEY>", "<ACCESS_TOKEN>", "<CHAIN_NAME>")bucket_client =BucketAPI(mcs_api)# Return true if bucket created, else Falsebucket_client.delete_bucket(bucket_name)
from swan_mcs import APIClient, BucketAPImcs_api =APIClient("<API_KEY>", "<ACCESS_TOKEN>", "<CHAIN_NAME>")bucket_client =BucketAPI(mcs_api)# Return None if MCS cannot find this bucketprint(bucket_client.get_bucket("<BUCKET_NAME>","<BUCKET_UID>").to_json())
Create an MCS Bucket
create_bucket(bucket_name)
Creates a bucket. Buckets under the same account cannot share the same name.
from swan_mcs import APIClient, BucketAPImcs_api =APIClient("<API_KEY>", "<ACCESS_TOKEN>", "<CHAIN_NAME>")bucket_client =BucketAPI(mcs_api)bucket_client.create_bucket("<BUCKET_NAME>")
Parameters
bucket_name: The name of the bucket
Return
Returns True if the bucket creation was successful
True
Get Buckets
list_buckets()
List all the existing buckets for the MCS account.
from swan_mcs import APIClient, BucketAPImcs_api =APIClient("<API_KEY>", "<ACCESS_TOKEN>", "<CHAIN_NAME>")bucket_client =BucketAPI(mcs_api)for i in bucket_client.list_buckets():print(i.to_json())
The create_folder the method allows you to create a folder in the specified bucket. It accepts buckets name, folder names, and prefixes. The folder will be created in bucket_name/prefix/folder_name
from swan_mcs import APIClient, BucketAPImcs_api =APIClient("<API_KEY>", "<ACCESS_TOKEN>", "<CHAIN_NAME>")bucket_client =BucketAPI(mcs_api)# If you want to place the folder directly in the root directory, you can leave the prefix field emptybucket_client.create_folder("<BUCKET_NAME>","<FOLDER_NAME>","<PREFIX>")
Parameters
bucket_name: The name of the bucket
folder_name: The name of the folder to create
prefix: The prefix for the folder's object name
Return
Returns True if the folder creation was successful
Uploads a file to a bucket. Use the bucket_name to select the bucket, and object_name to select the path and file name of the uploaded file. file_path is the local path of the file.
from swan_mcs import APIClient, BucketAPImcs_api =APIClient("<API_KEY>", "<ACCESS_TOKEN>", "<CHAIN_NAME>")bucket_client =BucketAPI(mcs_api)# Object name is the destination path you want to upload to the bucket# For example, if you want to upload the testfile.json file to path 111/222 in the TEST bucket, you would write: # bucket_client.upload_file("TEST", "111/222/testfile.json", "<FILE_PATH>")bucket_client.upload_file("<BUCKET_NAME>", "<OBJECT_NAME>", "<FILE_PATH>")
Parameters
bucket_name: The name of the bucket
object_name: The object name of the file ex. 'folder1/file.png' will upload the file as file.png inside bucket_name/folder1
file_path: The local file path
replace: File's of the same name cannot be uploaded. When replace is set to True, it will overwrite the previous file of the same name.
Get a list of file information from one of your buckets
from swan_mcs import APIClient, BucketAPImcs_api =APIClient("<API_KEY>", "<ACCESS_TOKEN>", "<CHAIN_NAME>")bucket_client =BucketAPI(mcs_api)for i in api.list_files("<BUCKET_NAME>", '<PREFIX>', "<LIMIT>", '<OFFSET>'):print(i.to_json())