Skip to content

s3-cmd

versioning

create s3 with versioning

bucket_name=p1panlm
aws_region=us-east-2
aws s3api create-bucket --bucket $bucket_name \
--create-bucket-configuration LocationConstraint=${aws_region} 

aws s3api put-bucket-versioning --bucket $bucket_name --versioning-configuration Status=Enabled

delete s3 with versioning enabled

bucket_name=aws-codestar-ap-southeast-1-xxxxxx-proj1-pipe
aws s3api put-bucket-versioning --bucket $bucket_name --versioning-configuration Status=Suspended
aws s3api delete-objects \
      --bucket $bucket_name \
      --delete "$(aws s3api list-object-versions \
      --bucket $bucket_name | \
      jq '{Objects: [.Versions[] | {Key:.Key, VersionId : .VersionId}], Quiet: false}')"

delete s3 without versioning

for i in $a ; do 
aws s3 rm s3://$i --region us-east-1 --recursive
aws s3 rb s3://$i --force --region us-east-1
done

aws s3api delete-objects --bucket panlm-test-object-1234 --delete '{"Objects":[{"Key":"def"}]}'

 ```

## download s3 folder
```sh
aws s3 sync s3://my-exported-logs .

create folder

aws s3api put-object \
--bucket ${bucket_name} \
--key ${folder_name}/

head object

aws s3api head-object --bucket lcf-1350 --key stop_sensor_data.sh

# sample output: 
{
    "AcceptRanges": "bytes",
    "Expiration": "expiry-date=\"Thu, 02 Nov 2023 00:00:00 GMT\", rule-id=\"rule1\"",
    "LastModified": "2023-10-22T09:41:20+00:00",
    "ContentLength": 162,
    "ETag": "\"65c759947d7b4e98624fa5bec23e0df0\"",
    "ContentType": "text/x-sh",
    "ServerSideEncryption": "AES256",
    "Metadata": {}
}
  • ETag, is md5
  • Expiration, when you has rule for this object
  • LastModified, only timestamp

get object

get object

aws s3api get-object \
  --key results/15c2c468a4c4.txt \
  --bucket athena-bucket-1115 \
  --region us-east-2 \
  download.txt

get object from access point

# using access point alias
aws s3api get-object \
  --key results/15c2c468a4c4.txt \
  --bucket arn:aws:s3:us-east-2:ACCOUNT_ID:accesspoint/testap-internet \
  --region us-east-2 \
  download.txt

update access point policy

aws s3control get-access-point-policy \
  --region us-east-2 \
  --account-id ACCOUNT_ID \
  --name testap-internet
aws s3control put-access-point-policy \
  --region us-east-2 \
  --account-id ACCOUNT_ID \
  --name testap-internet \
  --policy file://policy.json

presigned url

for download

OBJECT_KEY="folder/subfolder/file.txt"
EXPIRES=3600 # max 7 days
aws s3 presign s3://my-bucket/${OBJECT_KEY} --expires-in ${EXPIRES} --region xxx # keep region same with bucket

for upload


public-access-

delete obj

export BUCKET_NAME=perspective-0106-xxx
aws s3api list-object-versions --bucket ${BUCKET_NAME} --output json --query "Versions[].{Key:Key,VersionId:VersionId}" | jq -r '.[] | "aws s3api delete-object --bucket ${BUCKET_NAME} --key \(.Key) --version-id \(.VersionId)"' | sh

aws s3api list-object-versions --bucket ${BUCKET_NAME} --output json --query "DeleteMarkers[].{Key:Key,VersionId:VersionId}" | jq -r '.[] | "aws s3api delete-object --bucket ${BUCKET_NAME} --key \(.Key) --version-id \(.VersionId)"' | sh

aws s3 rb s3://${BUCKET_NAME}  --force