Help us understand the problem. What is going on with this article?

boto3でS3のファイルを操作

More than 3 years have passed since last update.

環境

  • mac os x 10.10.5
  • python 3.5.0
  • pip 7.1.2

準備

boto3インストール


$pip install boto3

credentials設定(e.g. ~/.aws/credentials)

[default]
aws_access_key_id = YOUR_KEY
aws_secret_access_key = YOUR_SECRET

地域設定 (e.g. ~/.aws/config):

[default]
region=eu-central-1

アップロード

import boto3

s3_client = boto3.client('s3')

# Upload the file to S3
s3_client.upload_file('test.txt', 'bucket-name', 'test-remote.txt')

情報取得

import boto3

s3 = boto3.resource('s3')
bucket = s3.Bucket('bucket-name')

for object in bucket.objects.all():
    print(object.key)

古いファイルを削除

import boto3
import datetime as dt

s3 = boto3.resource('s3')
retention_period = 100

bucket = s3.Bucket('bucket-name')

# check each file if it is expired or not
for object in bucket.objects.all():
    gap = dt.datetime.now(dt.timezone.utc) - object.last_modified

    if gap.days > retention_period:
        object.delete()

随時追加していきます。

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした