Skip to main content

Uploading images via Object Storage API

This guide explains how to upload an image to Object Storage using Python code and the KakaoCloud Object Storage API.
KakaoCloud allows users to manage Object Storage programmatically via the Object Storage API. This guide provides an example of how to manage Object Storage buckets and objects using Python code to call the Object Storage API.

info
  • Estimated time: 20 minutes
  • Region: kr-central-2
  • Prerequisites
    • Python environment setup
    • pip: Python package management tool

Prework

Get access key

The access key, which consists of an Access Key ID and a Secret Access Key, is required for issuing API authentication tokens as part of the IAM user credentials.
Refer to the API usage preparation document and follow the steps in the Get access key guide to get access keys.

caution

Once you close the popup, the Access key ID and Secret access key information cannot be retrieved again. Therefore, it's advised to copy and securely store this information.

Issue API authentication token

The API authentication token replaces your KakaoCloud account ID and password and allows you to authenticate applications or services via CLI or API.
After issuing the access key, you can use the Access Key ID and Secret Access Key to obtain an API authentication token.

Refer to the API usage preparation document and follow the steps in Get API authentication token to issue the token.

info

By default, API authentication tokens expire after 12 hours, although they may expire or change sooner depending on the situation.

Install Python requests module

Use the pip command to install the Python requests module, which is used for handling HTTP requests in Python.

pip install requests

Manage buckets via API calls

info

In this guide, we will write the API call code in Python. Create a Python file (.py) and perform the tasks in that file.

Step 1. Create bucket

  1. Write the API call code to create a bucket in Object Storage.

    Create a bucket
    import requests
    import json

    def createBucket():
    headers = {
    'X-Auth-Token': '{X_AUTH_TOKEN}',
    'Content-Type': 'application/json'
    }
    data = {
    "name": "{BUCKET_NAME}",
    "type": "STANDARD",
    "use_encryption": true,
    "encryption_configuration": {
    "type": "managed"
    }
    }

    res = requests.put('https://objectstorage.kr-central-2.kakaocloud.com/v1_ext/bucket', headers=headers, data=json.dumps(data))
    print(res.status_code)

    createBucket()
    VariableDescription
    X_AUTH_TOKENAPI authentication token
    BUCKET_NAMEName of the bucket to be created
  2. The result of the execution will output the Status code of the HTTP request. You can check the result of the request execution through the Status value.

    Response Status Code information for bucket creation requests
    HTTP StatusResponseDescription
    201     OK BucketSuccess
    401UnauthorizedAuthorization failed
    409ConflictsCreation failed
    - A bucket with the same name already exists
  3. You can verify the created bucket in the bucket list of the KakaoCloud Console > Object Storage service.

Step 2. Manage bucket access

  1. Write the API call code to manage access to the bucket. In this example, public access for all IP addresses is allowed for the bucket.

    API call to manage bucket access
    import requests
    import json

    def manageBucketAccess():
    headers = {
    'X-Auth-Token': '{X_AUTH_TOKEN}',
    'Content-Type': 'application/json'
    }
    data = {
    "name": "{BUCKET_NAME}",
    "acl": {
    "public": "read-only"
    }
    }

    res = requests.post('https://objectstorage.kr-central-2.kakaocloud.com/v1_ext/bucket/{BUCKET_NAME}', headers=headers, data=json.dumps(data))
    print(res.status_code)

    manageBucketAccess()
  2. The result of the execution will output the Status code of the HTTP request. You can check the result of the request execution through the Status value.

    Response Status Code information for bucket access management requests
    HTTP StatusResponseDescription
    200OK BucketSuccess
    401UnauthorizedAuthorization failed
    404Not foundBucket not found

Manage objects (File & Folder) via API calls

Step 1. Create a folder

  1. Write the API call code to create a folder inside the created bucket.

    API call to create a folder
    import requests

    def createFolder():
    headers = {
    'X-Auth-Token': '{X_AUTH_TOKEN}',
    'Content-Type': 'application/directory',
    'X-Object-Meta-Company': 'kakao enterprise'
    }
    res = requests.put('https://objectstorage.kr-central-2.kakaocloud.com/v1/{ACCOUNT}/{BUCKET_NAME}/{FOLDER_NAME}/', headers=headers)

    createFolder()
    VariableDescription
    ACCOUNTProject ID
    - Can be checked when issuing the token
    - Value of token > project > id in the response body
    FOLDER_NAMEName of the folder to be created
  2. After running the code, go to the KakaoCloud Console > Object Storage service and select the bucket. You can verify the created folder under the [Objects] tab.

    OSAPI 이미지업로드_폴더 생성 확인.png

Step 2. Upload an image file

  1. Write the API call code to upload a file into the created folder. Prepare a jpeg image file in your local environment to upload to Object Storage.

    API call to upload a file
    import requests

    def uploadFile():
    headers = {
    'X-Auth-Token': '{X_AUTH_TOKEN}',
    'Content-Type': 'image/jpeg'
    }
    data = open('{FILE_PATH}', 'rb')
    res = requests.put('https://objectstorage.kr-central-2.kakaocloud.com/v1/{ACCOUNT}/{BUCKET_NAME}/{FOLDER_NAME}/{FILE_NAME}', headers=headers, data=data)
    data.close()

    uploadFile()
    VariableDescription
    FILE_PATHPath to the file (in this exercise, the path to the jpeg file)
    FILE_NAMEName of the file to be created (uploaded)
  2. After running the code, go to the KakaoCloud Console > Object Storage service, click on the bucket, and check the [Objects] tab. You should see the test.jpeg file uploaded inside the folder created in Step 1.

  3. Copy the location URL from the file information of the uploaded image. You can access the image file by navigating to this URL.