カテゴリー
【AWS API入門】CurlコマンドでAWS APIを利用したS3バケットの操作の基礎を理解しよう
※ 当ページには【広告/PR】を含む場合があります。
2024/12/26
ListBuckets相当のAPIコマンド
アクセスキーID(AWS_ACCESS_KEY_ID)
アクセス秘密鍵(AWS_SECRET_ACCESS_KEY)
ap-northeast-1
https://s3.<リージョン名>.amazonaws.com/
$ curl -XGET "https://s3.ap-northeast-1.amazonaws.com/" \
--aws-sigv4 "aws:amz:ap-northeast-1:s3" \
--user "${AWS_ACCESS_KEY_ID}:${AWS_SECRET_ACCESS_KEY}" \
-vs
#👇出力例
* Host s3.ap-northeast-1.amazonaws.com:443 was resolved.
* IPv6: (none)
* IPv4: xxx.xxx.xxx.xxx,...
* Trying xxx.xxx.xxx.xxx:443...
* Connected to s3.ap-northeast-1.amazonaws.com (xxx.xxx.xxx.xxx) port 443
* ALPN: curl offers h2,http/1.1
* TLSv1.3 (OUT), TLS handshake, Client hello (1):
* CAfile: /etc/ssl/certs/ca-certificates.crt
* CApath: /etc/ssl/certs
* TLSv1.3 (IN), TLS handshake, Server hello (2):
* TLSv1.3 (IN), TLS handshake, Encrypted Extensions (8):
* TLSv1.3 (IN), TLS handshake, Certificate (11):
* TLSv1.3 (IN), TLS handshake, CERT verify (15):
* TLSv1.3 (IN), TLS handshake, Finished (20):
* TLSv1.3 (OUT), TLS change cipher, Change cipher spec (1):
* TLSv1.3 (OUT), TLS handshake, Finished (20):
* SSL connection using TLSv1.3 / TLS_AES_128_GCM_SHA256 / x25519 / RSASSA-PSS
* ALPN: server accepted http/1.1
* Server certificate:
* subject: CN=*.s3-ap-northeast-1.amazonaws.com
* start date: Oct 26 00:00:00 2024 GMT
* expire date: Oct 25 23:59:59 2025 GMT
* subjectAltName: host "s3.ap-northeast-1.amazonaws.com" matched cert's "s3.ap-northeast-1.amazonaws.com"
* issuer: C=US; O=Amazon; CN=Amazon RSA 2048 M01
* SSL certificate verify ok.
* Certificate level 0: Public key type RSA (2048/112 Bits/secBits), signed using sha256WithRSAEncryption
* Certificate level 1: Public key type RSA (2048/112 Bits/secBits), signed using sha256WithRSAEncryption
* Certificate level 2: Public key type RSA (2048/112 Bits/secBits), signed using sha256WithRSAEncryption
* using HTTP/1.x
* Server auth using AWS_SIGV4 with user 'XXXXXXXXXXXXXXXXXXXX'
> GET / HTTP/1.1
> Host: s3.ap-northeast-1.amazonaws.com
> Authorization: AWS4-HMAC-SHA256 Credential=XXXXXXXXXXXXXXXXXXXX/20241224/ap-northeast-1/s3/aws4_request, SignedHeaders=host;x-amz-content-sha256;x-amz-date, Signature=7xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxf
> X-Amz-Date: 20241224T071811Z
> x-amz-content-sha256: exxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx5
> User-Agent: curl/8.9.0
> Accept: */*
>
* Request completely sent off
< HTTP/1.1 200 OK
< x-amz-id-2: /+TxxxxxxxxF/Cxxxxxxxxxxxxxxxxxxxxxxxxxf/uxxxxxxxi
< x-amz-request-id: 4xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx4
< Date: Tue, 24 Dec 2024 07:21:52 GMT
< Content-Type: application/xml
< Transfer-Encoding: chunked
< Server: AmazonS3
<
<?xml version="1.0" encoding="UTF-8"?>
<ListAllMyBucketsResult xmlns="http://s3.amazonaws.com/doc/2006-03-01/"><Owner><ID>9xxxxxxxxxf</ID><DisplayName>xxxxxxxxx</DisplayName></Owner><Buckets><Bucket><Name>xxxxxxxxxxxx</Name><CreationDate>2024-11-09T01:42:18.000Z</CreationDate></Bucket>...</Buckets></ListAllMyBucketsResult>* Connection #0 to host s3.ap-northeast-1.amazonaws.com left intact
--aws-sigv4 "aws:amz:ap-northeast-1:s3"
"aws:amz:ap-northeast-1:s3"
Authorization
--user "<アクセスキーID>:<アクセス秘密鍵>"
補遺〜RoleからAWSクレデンシャルを得る
LambdaFullAccess
assume-role
$ session=$(aws sts assume-role \
--role-arn arn:aws:iam::<AWSのアカウントID>:role/LambdaFullAccess \
--role-session-name "role_session"
)
#👇取得したクレデンシャルを変数にセット
$ AWS_ACCESS_KEY_ID=$(echo "${session}" | jq -r ".Credentials.AccessKeyId")
$ AWS_SECRET_ACCESS_KEY=$(echo "${session}" | jq -r ".Credentials.SecretAccessKey")
$ AWS_SESSION_TOKEN=$(echo "${session}" | jq -r ".Credentials.SessionToken")
CreateBucket相当のAPIコマンド
ap-northeast-1
hoge-hoge-hoge
$ curl -XPUT "https://hoge-hoge-hoge.s3.ap-northeast-1.amazonaws.com/" \
--header "x-amz-date: $(date -u +%Y%m%dT%H%M%SZ)" \
--header "Content-Type: application/xml" --data "<CreateBucketConfiguration xmlns=\"http://s3.amazonaws.com/doc/2006-03-01/\"><LocationConstraint>ap-northeast-1</LocationConstraint></CreateBucketConfiguration>" \
--aws-sigv4 "aws:amz:ap-northeast-1:s3" \
--user "${AWS_ACCESS_KEY_ID}:${AWS_SECRET_ACCESS_KEY}" \
-vs
#👇結果
* Host hoge-hoge-hoge.s3.ap-northeast-1.amazonaws.com:443 was resolved.
* IPv6: (none)
* IPv4: xxx.xxx.xxx.xxx, ...
* Trying xxx.xxx.xxx.xxx:443...
* Connected to hoge-hoge-hoge.s3.ap-northeast-1.amazonaws.com (xxx.xxx.xxx.xxx) port 443
* ALPN: curl offers h2,http/1.1
* TLSv1.3 (OUT), TLS handshake, Client hello (1):
* CAfile: /etc/ssl/certs/ca-certificates.crt
* CApath: /etc/ssl/certs
* TLSv1.3 (IN), TLS handshake, Server hello (2):
* TLSv1.3 (IN), TLS handshake, Encrypted Extensions (8):
* TLSv1.3 (IN), TLS handshake, Certificate (11):
* TLSv1.3 (IN), TLS handshake, CERT verify (15):
* TLSv1.3 (IN), TLS handshake, Finished (20):
* TLSv1.3 (OUT), TLS change cipher, Change cipher spec (1):
* TLSv1.3 (OUT), TLS handshake, Finished (20):
* SSL connection using TLSv1.3 / TLS_AES_128_GCM_SHA256 / x25519 / RSASSA-PSS
* ALPN: server accepted http/1.1
* Server certificate:
* subject: CN=*.s3-ap-northeast-1.amazonaws.com
* start date: Oct 26 00:00:00 2024 GMT
* expire date: Oct 25 23:59:59 2025 GMT
* subjectAltName: host "hoge-hoge-hoge.s3.ap-northeast-1.amazonaws.com" matched cert's "*.s3.ap-northeast-1.amazonaws.com"
* issuer: C=US; O=Amazon; CN=Amazon RSA 2048 M01
* SSL certificate verify ok.
* Certificate level 0: Public key type RSA (2048/112 Bits/secBits), signed using sha256WithRSAEncryption
* Certificate level 1: Public key type RSA (2048/112 Bits/secBits), signed using sha256WithRSAEncryption
* Certificate level 2: Public key type RSA (2048/112 Bits/secBits), signed using sha256WithRSAEncryption
* using HTTP/1.x
* Server auth using AWS_SIGV4 with user 'XXXXXXXXXXXXXXXXXXXX'
> PUT / HTTP/1.1
> Host: hoge-hoge-hoge.s3.ap-northeast-1.amazonaws.com
> Authorization: AWS4-HMAC-SHA256 Credential=XXXXXXXXXXXXXXXXXXXX/20241224/ap-northeast-1/s3/aws4_request, SignedHeaders=content-type;host;x-amz-content-sha256;x-amz-date, Signature=bxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx7
> x-amz-content-sha256: 2xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx2
> User-Agent: curl/8.9.0
> Accept: */*
> x-amz-date: 20241224T124136Z
> Content-Type: application/xml
> Content-Length: 159
>
* upload completely sent off: 159 bytes
< HTTP/1.1 200 OK
< x-amz-id-2: WxxxxxxxxxxxR/sxxxxxxxxxxY/lxxxxxxxxxxxJ/Vxxxxxxxxxxx3/Wxxxxxxxxxxxxxxxxx=
< x-amz-request-id: 2xxxxxxxxxxxxxxxxS
< Date: Tue, 24 Dec 2024 12:41:41 GMT
< Location: http://hoge-hoge-hoge.s3.amazonaws.com/
< Content-Length: 0
< Server: AmazonS3
<
* Connection #0 to host hoge-hoge-hoge.s3.ap-northeast-1.amazonaws.com left intact
ap-northeast-1
hoge-hoge-hoge
https://<バケット名>.s3.ap-northeast-1.amazonaws.com/
<バケット名>
--header "x-amz-date: <DATE>"
<DATE>
補遺〜AWS APIのDateヘッダーとx-amz-dateヘッダーの区別
Date:
The date that can be used to create the signature contained in the Authorization header.
If the Date header is to be used for signing it must be specified in the ISO 8601 basic format.
In this case, the x-amz-date header is not needed.
Note that when x-amz-date is present, it always overrides the value of the Date header.
If the Date header is not used for signing, it can be one of the full date formats specified by RFC 2616, section 3.3.
For example, the date/time Wed, 01 Mar 2006 12:00:00 GMT is a valid date/time header for use with Amazon S3.
If you are using the Date header for signing, then it must be in the ISO 8601 basic YYYYMMDD'T'HHMMSS'Z' format.
If Date is specified but is not in ISO 8601 basic format, then you must also include the x-amz-date header.
If Date is specified in ISO 8601 basic format, then this is sufficient for signing requests and you do not need the x-amz-date header.
For more information, see Handling Dates in Signature Version 4 in the Amazon Web Services Glossary.
x-amz-date:
The date used to create the signature in the Authorization header.
The format must be ISO 8601 basic in the YYYYMMDD'T'HHMMSS'Z' format.
For example, the date/time 20170210T120000Z is a valid x-amz-date for use with Amazon S3.
x-amz-date is optional for all requests; it can be used to override the date used for signing requests. If the Date header is specified in the ISO 8601 basic format, then x-amz-date is not needed. When x-amz-date is present, it always overrides the value of the Date header. For more information, see Handling Dates in Signature Version 4 in the Amazon Web Services Glossary.
+ SigV4の署名としてDateヘッダーを使うなら、「ISO8601」形式(YYYYMMDD'T'HHMMSS'Z')に従うこと
+ DateヘッダーをISO8601形式とした場合、x-amz-dateヘッダーは必要ない
+ x-amz-dateが存在する場合はこちらが優先され、Dateヘッダーの値は上書きされる
+ DateヘッダーをSigV4の署名情報として利用したくない場合、
値をISO8601形式以外に設定し、x-amz-dateヘッダーにはISO8601形式の日付を与えること
$ date -u +"%Y%m%dT%H%M%SZ"
20241224T075233Z
DeleteBucket相当のコマンド
DELETE
hoge-hoge-hoge
$ curl -XDELETE "https://hoge-hoge-hoge.s3.ap-northeast-1.amazonaws.com/" \
--header "x-amz-date: $(date -u +%Y%m%dT%H%M%SZ)" \
--aws-sigv4 "aws:amz:ap-northeast-1:s3" \
--user "${AWS_ACCESS_KEY_ID}:${AWS_SECRET_ACCESS_KEY}" \
-vs
#👇結果
* Host hoge-hoge-hoge.s3.ap-northeast-1.amazonaws.com:443 was resolved.
* IPv6: (none)
* IPv4: xxx.xxx.xxx.xxx, xxx.xxx.xxx.xxx, xxx.xxx.xxx.xxx, xxx.xxx.xxx.xxx, xxx.xxx.xxx.xxx, xxx.xxx.xxx.xxx, xxx.xxx.xxx.xxx, xxx.xxx.xxx.xxx
* Trying xxx.xxx.xxx.xxx:443...
* Connected to hoge-hoge-hoge.s3.ap-northeast-1.amazonaws.com (xxx.xxx.xxx.xxx) port 443
* ALPN: curl offers h2,http/1.1
* TLSv1.3 (OUT), TLS handshake, Client hello (1):
* CAfile: /etc/ssl/certs/ca-certificates.crt
* CApath: /etc/ssl/certs
* TLSv1.3 (IN), TLS handshake, Server hello (2):
* TLSv1.3 (IN), TLS handshake, Encrypted Extensions (8):
* TLSv1.3 (IN), TLS handshake, Certificate (11):
* TLSv1.3 (IN), TLS handshake, CERT verify (15):
* TLSv1.3 (IN), TLS handshake, Finished (20):
* TLSv1.3 (OUT), TLS change cipher, Change cipher spec (1):
* TLSv1.3 (OUT), TLS handshake, Finished (20):
* SSL connection using TLSv1.3 / TLS_AES_128_GCM_SHA256 / x25519 / RSASSA-PSS
* ALPN: server accepted http/1.1
* Server certificate:
* subject: CN=*.s3-ap-northeast-1.amazonaws.com
* start date: Oct 26 00:00:00 2024 GMT
* expire date: Oct 25 23:59:59 2025 GMT
* subjectAltName: host "hoge-hoge-hoge.s3.ap-northeast-1.amazonaws.com" matched cert's "*.s3.ap-northeast-1.amazonaws.com"
* issuer: C=US; O=Amazon; CN=Amazon RSA 2048 M01
* SSL certificate verify ok.
* Certificate level 0: Public key type RSA (2048/112 Bits/secBits), signed using sha256WithRSAEncryption
* Certificate level 1: Public key type RSA (2048/112 Bits/secBits), signed using sha256WithRSAEncryption
* Certificate level 2: Public key type RSA (2048/112 Bits/secBits), signed using sha256WithRSAEncryption
* using HTTP/1.x
* Server auth using AWS_SIGV4 with user 'XXXXXXXXXXXXXXXXXXXX'
> DELETE / HTTP/1.1
> Host: hoge-hoge-hoge.s3.ap-northeast-1.amazonaws.com
> Authorization: AWS4-HMAC-SHA256 Credential=XXXXXXXXXXXXXXXXXXXX/20241224/ap-northeast-1/s3/aws4_request, SignedHeaders=host;x-amz-content-sha256;x-amz-date, Signature=3xxxxxxxxxxxxxxxxxxxxxxxx5
> x-amz-content-sha256: exxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx5
> User-Agent: curl/8.9.0
> Accept: */*
> x-amz-date: 20241224T161715Z
>
* Request completely sent off
< HTTP/1.1 204 No Content
< x-amz-id-2: Qxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx=
< x-amz-request-id: 7xxxxxxxxxxxxxxxxxxQ
< Date: Tue, 24 Dec 2024 16:17:20 GMT
< Server: AmazonS3
<
* Connection #0 to host hoge-hoge-hoge.s3.ap-northeast-1.amazonaws.com left intact
まとめ
Listbuckets
CreateBucket
DeleteBucket
参考サイト
記事を書いた人
ナンデモ系エンジニア
主にAngularでフロントエンド開発することが多いです。 開発環境はLinuxメインで進めているので、シェルコマンドも多用しております。 コツコツとプログラミングするのが好きな人間です。
カテゴリー