s3 api接口的挪用

时间:2021-10-13 08:25:10

比来公司使用s3做文件存储处事器,因此在措施中需要挪用s3的api,目前措施中使用了python和java版本的s3的api,简单做下记录,便利以后使用。

一、s3 api使用python版

1.安置boto3的sdk

pip install boto3==1.4.7

2.创建s3的api客户端

import boto3
from botocore.client import Config
import os
import sys
import threading

#创建s3链接,如果s3处事器是第四代,则需要设置signature_version=‘s3v4‘
s3_client = boto3.client(‘s3‘, endpoint_url=‘s3处事器地点‘,
                         aws_access_key_id=‘s3处事器的access_key_id‘,
                         aws_secret_access_key=‘s3处事器的secret_access_key‘,
                         region_name=‘s3处事器的时区,这里可以填写cn-north-1‘,
                         config=Config(signature_version=‘s3‘))

3.获取s3中bucket列表

bucket_list = s3_client.list_buckets()

print bucket_list

4.创建bucket

bucket = s3_client.create_bucket(Bucket=‘bucket的名称‘)

print bucket

5.获取bucket信息
bucket_info = s3_client.head_bucket(Bucket=‘bucket的名称‘)

print bucket_info

6.删除bucket
#bucket_delete = s3_client.delete_bucket(Bucket=‘bucket的名称‘)

7.上传文件到s3处事器

# s3_client.upload_file("上传的源文件地点", "bucket名称", "上传文件在s3上对应的key名称", ExtraArgs={‘ACL‘: ‘public-read‘})

或者

s3_client.upload_file("上传的源文件地点", "bucket名称", "上传文件在s3上对应的key名称",

      ExtraArgs={‘ACL‘: ‘public-read‘},

      Callback=UploadProgressPercentage("上传的源文件地点"))
Callback属性对应的类要领如下,该类要领在控制台中打印了上传文件的进度

class UploadProgressPercentage(object):
    def __init__(self, filename):
        self._filename = filename
        self._size = float(os.path.getsize(filename))
        self._seen_so_far = 0
        self._lock = threading.Lock()
    def __call__(self, bytes_amount):
        # To simplify we‘ll assume this is hooked up
        # to a single filename.
        with self._lock:
            self._seen_so_far += bytes_amount
            percentage = (self._seen_so_far / self._size) * 100
            sys.stdout.write(
                "\r%s  %s / %s  (%.2f%%)" % (
                    self._filename, self._seen_so_far, self._size,
                    percentage))
            sys.stdout.flush()

8.获取bucket下文件列表

object_list = s3_client.list_objects(Bucket=‘bucket名称‘)
print object_list

9.检察bucket下的某个文件信息
object_info = s3_client.get_object(Bucket=‘bucket名称‘, Key=‘文件对应的key名称‘)
print object_info
10.删除文件
object_delete = s3_client.delete_object(Bucket=‘bucket名称‘, Key=‘文件对应的key名称‘)

11.下载文件

s3_client.download_file("bucket名称", "文件对应的key名称", "文件下载到的地点")

或者

s3client.download_file("bucket名称", "文件对应的key名称", "文件下载到的地点",Callback=DownloadProgressPercentage("文件下载到的地点"))

Callback属性对应的类要领如下,该类要领在控制台中打印了下载文件的进度

class _DownloadProgressPercentage(object):
    def __init__(self, filename):
        self._filename = filename
        self._seen_so_far = 0
        self._lock = threading.Lock()
    def __call__(self, bytes_amount):
        # To simplify we‘ll assume this is hooked up
        # to a single filename.
        with self._lock:
            self._seen_so_far += bytes_amount
            sys.stdout.write(
                "\r%s --> %s bytes transferred" % (
                    self._filename, self._seen_so_far))
            sys.stdout.flush()

参考文档

boto3 api官方文档:

1.s3的client的api

s3 api接口的挪用

2.s3的client的api上传下载示例

s3 api接口的挪用

二、3 api使用java版

1.在pom.xml中增加依赖包