16
19

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

PythonでAmazon S3からバケット名とファイル名(オブジェクト名)を取得する。

Last updated at Posted at 2019-09-27

バケット名を取得する

import boto3
s3 = boto3.client('s3')
# jsonになっている
response = s3.list_buckets()
for bucket in response['Buckets']:
    print(bucket['Name'])

ファイル名(オブジェクト名)を取得する

import boto3
s3 = boto3.client('s3')

response = s3.list_objects_v2(Bucket="バケット名を書く")
for object in response['Contents']: 
    print(object['Key'])

ファイルの内容を取得する

import boto3
s3 = boto3.client('s3')

body = s3.get_object(Bucket="バケット名を書く",Key="Objectの階層を書く")['Body'].read()
# バケット名がfruitsでバケットの直下にapple.jsonを置いている場合
# body = s3.get_object(Bucket="fruits",Key="apple.json")['Body'].read()
print(body.decode('utf-8'))
16
19
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
16
19

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?