玩命加载中 . . .

S3 Example


概述

日常工作中经常使用的S3相关的动作,诸如上传Object,携带metadata等,简单做个汇总。

示例代码

#-*-coding:UTF-8-*-

import boto

import boto.s3.connection
import os
from boto.s3.key import Key
access_key ='XX4SO4LP5SDY3DZ0G0KN'
secret_key = 'tvRtXW/ADPUNSw9ccuMWoU1cFu/wJG2QbCOvbX34'


#建立连接
print "createt connection BEGIN \n"
conn = boto.connect_s3(
        aws_access_key_id = access_key,
        aws_secret_access_key = secret_key,
        host = '10.16.17.88',
        #is_secure=False,    #uncomment if you are not using ssl
        calling_format = boto.s3.connection.OrdinaryCallingFormat(),
        )
print conn

print "createt connection  END\n"

#列出所有已有的bucket,以及创建时间
print "list all the bucket in this account, empty of course"
for bucket in conn.get_all_buckets():
    print "{name}\t{created}".format(
            name = bucket.name,
            created = bucket.creation_date,
            )
print "list all buckets finished\n"

# 查找bucket是否存在
print "create bucket and list all bucket BEGIN"
bucket_name = "my-bucket"
bucket = conn.lookup(bucket_name)
if bucket == None :
    print "bucket {} is not exists".format(bucket_name)
    # 创建bucket 
    bucket = conn.create_bucket(bucket_name)
    # 再次列出所有的bucket,查看是否存在新建的bucket
    for bucket in conn.get_all_buckets():
        print "{name}\t{created}".format(
                name = bucket.name,
                created = bucket.creation_date,
                )
    print "create bucket and list all bucket END\n"

#查看bucket中的所有对象
print "list all the OBJECTS in bucket, empty of course"
bucket = conn.lookup(bucket_name)
for key in bucket.list():
    print "{name}\t{size}\t{modified}".format(
                name = key.name,
                size = key.size,
                modified =
                key.last_modified,
           )
print "list all the OBJECTS in bucket END\n"
   
# 上传对象,对象的内容来自字符串
key_name = "my-object"

print "upload object from string ,and list it "
key = bucket.lookup(key_name)
if key == None:
    print "key {} is not exists in bucket {}".format(key_name,bucket_name)
    k = Key(bucket)
    k.key = key_name
    k.set_contents_from_string("this object is generated by string")
    for key in bucket.list():
        print "{name}\t{size}\t{modified}".format(
                    name = key.name,
                    size = key.size,
                    modified =
                    key.last_modified,
               )

print "upload object from string ,and list it END\n "

# 上传对象,对象的内容来自文件
key_name = "my-object-from-file"

print "upload object from filename ,and list it "
#需要上传文件的路径,我的例子是Linux的路径,Windows下用文件的路径即可。
filename_need_upload = "/var/log/kern.log"
key = bucket.lookup(key_name)
if key == None:
    print "key {} is not exists in bucket {}".format(key_name,bucket_name)
    k = Key(bucket)
    k.key = key_name
    k.set_contents_from_filename(filename_need_upload)

    #上传完毕后,列出所有的对象,查看新上传的文件是否存在。
    for key in bucket.list():
        print "{name}\t{size}\t{modified}".format(
                    name = key.name,
                    size = key.size,
                    modified =
                    key.last_modified,
               )

print "upload object from file ,and list it END\n"
# 下载刚刚上传的对象 
key_name = "my-object-from-file"
filename_of_download = "/tmp/kern.log.bak"


print "download object to file string ,and check file "
key = bucket.get_key(key_name)
key.get_contents_to_filename(filename_of_download)

if os.path.isfile(filename_of_download):
    print "{} is file ,download successfully".format(filename_of_download)

print "download object to file string ,and check file END\n "


# 删除bucket中的某个对象

key_name = "my-object-from-file"
bucket.delete_key(key_name)

# 删除之后查看对应的key 是否存在
for key in bucket.list():
    print "{name}\t{size}\t{modified}".format(
                name = key.name,
                size = key.size,
                modified =
                key.last_modified,
           )

# 删除bucket中所有的objcet
for key in bucket.list():
    bucket.delete_key(key.name)

# 删除bucket
bucket_name = "my-bucket"
conn.delete_bucket(bucket_name)

#!/usr/bin/env python
#-*-coding:UTF-8-*-

import boto3

s3_client = boto3.client('10.16.17.11:8080')
open('hello.txt').write('Hello, world!')

# Upload the file to S3
s3_client.upload_file('hello.txt', 'BUCKET', 'hello-remote.txt')

# Download the file from S3
s3_client.download_file('BUCKET', 'hello-remote.txt', 'hello2.txt')
print(open('hello2.txt').read())


文章作者: Gavin Wang
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 Gavin Wang !
  目录