brunch

매거진 kakao cloud

You can make anything
by writing

C.S.Lewis

by Master Seo Feb 23. 2024

카카오 클라우드 5탄-11.API로 오브젝트 스토리지

API로 오브젝트 스토리지 관리해 보자.


사전준비 = Access-key



<1> 유틸과 환경 설정

<2> 버킷 만들기



<1> 유틸과 환경 설정


1

베스천에서  유틸 설치



sudo yum update -y

sudo yum install -y epel-release 

sudo yum install -y jq

sudo yum install -y unzip

sudo curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"

sudo unzip awscliv2.zip

sudo ./aws/install



2

# USER_ID는 오른쪽위 계정 , 사용자 고유 ID로 확인한다.



cat << 'EOF' > env.sh

export ACCESS_KEY="{KAKAO_ACCESS_KEY}"

export ACCESS_SECRET_KEY="{KAKAO_ACCESS_SECRET_KEY}"

export USER_ID="{USER_ID}"

export AWS_DEFAULT_REGION="kr-central-2"

export AWS_ENDPOINT_URL="https://objectstorage.kr-central-2.kakaoi.io"


export TOKEN=$(curl -s -X POST -i https://iam.kakaoi.io/identity/v3/auth/tokens -H "Content-Type: application/json" -d \

'{

    "auth": {

        "identity": {

            "methods": [

                "application_credential"

            ],

            "application_credential": {

                "id": "'${ACCESS_KEY}'",

                "secret": "'${ACCESS_SECRET_KEY}'"

            }

        }

    }

}' | grep -i X-Subject-Token | awk -v RS='\r\n' '{print $2}')

if [ -z $TOKEN ]; then

        echo "TOKEN is null..."

fi

export PROJECT_ID=$(curl -s -X POST https://iam.kakaoi.io/identity/v3/auth/tokens -H "Content-Type: application/json" -d \

'{

    "auth": {

        "identity": {

            "methods": [

                "application_credential"

            ],

            "application_credential": {

                "id": "'${ACCESS_KEY}'",

                "secret": "'${ACCESS_SECRET_KEY}'"

            }

        }

    }

}' | jq -r ".token.project.id")

if [ -z $PROJECT_ID ]; then

        echo "PROJECT_ID is null..."

fi


export CREDENTIALS=$(curl -s -X POST -i https://iam.kakaoi.io/identity/v3/users/${USER_ID}/credentials/OS-EC2 \

-H "Content-Type: application/json" \

-H "X-Auth-Token: ${TOKEN}" -d \

'{

    "tenant_id": "'${PROJECT_ID}'"

}' | awk '/\{/{flag=1} flag {print}')

export AWS_ACCESS_KEY_ID=$(echo "$CREDENTIALS" | jq -r '.credential.access')

export AWS_SECRET_ACCESS_KEY=$(echo "$CREDENTIALS" | jq -r '.credential.secret')

if [ -z $AWS_ACCESS_KEY_ID ]; then

        echo "AWS_ACCESS_KEY_ID is null..."

fi

if [ -z $AWS_SECRET_ACCESS_KEY ]; then

        echo "AWS_SECRET_ACCESS_KEY is null..."

fi

{


echo ACCESS_KEY=$ACCESS_KEY

echo ACCESS_SECRET_KEY=$ACCESS_SECRET_KEY

echo TOKEN=$TOKEN

echo PROJECT_ID=$PROJECT_ID

echo AWS_ACCESS_KEY_ID=$AWS_ACCESS_KEY_ID

echo AWS_SECRET_ACCESS_KEY=$AWS_SECRET_ACCESS_KEY

echo AWS_DEFAULT_REGION=$AWS_DEFAULT_REGION

echo AWS_ENDPOINT_URL=$AWS_ENDPOINT_URL

} | tee output.txt

EOF



3

 . env.sh




참고 사이트

https://github.com/kakaocloud-edu/tutorial/blob/main/EssentialBasicCourse/PracticalTextbook/Lab11.md




<2> 버킷 만들기



1


curl --request PUT --location "${AWS_ENDPOINT_URL}/v1_ext/bucket"  --header 'X-Auth-Token: "'${TOKEN}'"'  --header 'Content-Type: application/json'  --data '{

     "name": "bucket-test",

     "type": "STANDARD",

     "use_encryption": true,

     "encryption_configuration": {

         "type": "managed"

     }

 }' | jq '.'





  "project": "2016b0738b2a49259262926a6fe1ada6",

  "account": "2016b0738b2a49259262926a6fe1ada6",

  "name": "bucket-test",

  "type": "STANDARD",

  "bytes": 0,

  "objectCount": 0,

  "createdAt": "2024-02-23T06:23:47Z",

  "use_encryption": true,

  "encryption_type": "managed",

  "lastModified": "2024-02-23T06:23:47Z",

  "storageClass": "STANDARD",

  "storagePolicy": "default-placement",

  "acl": {

    "public": "deny",

    "public_read_allow_ip_list": null

  }

}



2

콘솔에서 확인



3

# 명령어로 만들어 보기


[centos@host-172-30-1-34 fs1]$ aws s3api create-bucket --bucket my-bucket


[centos@host-172-30-1-34 fs1]$ aws s3 ls

2024-02-23 06:23:47 bucket-test

2024-02-23 06:09:00 bucket1

2024-02-23 06:26:38 my-bucket



[centos@host-172-30-1-34 fs1]$ aws s3api put-object --bucket my-bucket --key my-folder/

{

    "ETag": "\"d41d8cd98f00b204e9800998ecf8427e\""

}


[centos@host-172-30-1-34 fs1]$ echo "This is my text" >> my-file.txt


[centos@host-172-30-1-34 fs1]$ aws s3 mv my-file.txt s3://my-bucket/my-folder/

move: ./my-file.txt to s3://my-bucket/my-folder/my-file.txt


[centos@host-172-30-1-34 fs1]$ aws s3 cp s3://my-bucket/my-folder/my-file.txt copytext.txt

download: s3://my-bucket/my-folder/my-file.txt to ./copytext.txt


[centos@host-172-30-1-34 fs1]$ cat copytext.txt

This is my text




# 삭제


[centos@host-172-30-1-34 fs1]$ aws s3 rm s3://my-bucket/my-folder/my-file.txt

delete: s3://my-bucket/my-folder/my-file.txt


[centos@host-172-30-1-34 fs1]$ aws s3 rm s3://my-bucket/my-folder/

delete: s3://my-bucket/my-folder/



[centos@host-172-30-1-34 fs1]$ aws s3 rb s3://my-bucket

remove_bucket: my-bucket






https://brunch.co.kr/@topasvga/3703


감사합니다.



keyword
매거진의 이전글 카카오 클라우드 5탄-10. 오브젝트 스토리지
브런치는 최신 브라우저에 최적화 되어있습니다. IE chrome safari