使用python操作Amazon S3

1.判断s3 object是否存在

import boto3

s3 = boto3.resource('s3')
bucket = s3.Bucket('my-bucket')
key = 'dootdoot.jpg'
objs = list(bucket.objects.filter(Prefix=key))
if any([w.key == path_s3 for w in objs]):
    print("Exists!")
else:
    print("Doesn't exist")

2.读取s3 object文件内容

import boto3

s3 = boto3.resource('s3')
object_content = s3.Object('my_bucket_name', object_dir)
file_content = object_content.get()['Body'].read().decode('utf-8')

3.列出s3 object目录

import boto3

s3 = boto3.resource('s3')
my_bucket = s3.Bucket('my_bucket_name')
dirs = sorted([_.key for _ in my_bucket.objects.filter(Prefix="aaa/bbb/ccc/")], reverse=True)
for dir in dirs:
    print(dir)

4.查看emr集群信息,参考:https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/emr.html#EMR.Client.describe_cluster

import boto3

emr_client = boto3.client('emr',region_name='us-west-2')
response = emr_client.describe_cluster(ClusterId="j-xxxxxx")
print(response['Cluster']['MasterPublicDnsName'])

  

  

posted @ 2016-04-04 11:17  tonglin0325  阅读(270)  评论(0编辑  收藏  举报