python上传大文件s3_aws s3上传大文件的4种方法

aws s3 上对存储的数据容量是没有限制的,各个 Amazon S3 数据元的大小可在 1 字节至 5 TB 之间,可在单个 PUT 中上传的最大数据元为 5 GB,对于大于 100 MB 的数据元,官方建议是要采用分段上传的方式,所以如果你有很大的文件要上传到S3上,而目前还不知道采用哪种方式最好,今天这篇文章我们就是专门来讨论如何把一个大文件上传到S3上的,经过我自己汇总,S3对上传大文件有四种方式,以下我们分别介绍:

1、第一种,用命令行方式上传,采用s3put命令进行上传:

s3put --bucket bicher --multipart --callback 5 pcre-8.32.zip

Copying /root/boto_aws/pcre-8.32.zip to bicher/root/boto_aws/pcre-8.32.zip

0 bytes transferred / 2030600 bytes total

507904 bytes transferred / 2030600 bytes total

1015808 bytes transferred / 2030600 bytes total

1523712 bytes transferred / 2030600 bytes total

2030600 bytes transferred / 2030600 bytes total

1

2

3

4

5

6

7

s3put--bucketbicher--multipart--callback5pcre-8.32.zip

Copying/root/boto_aws/pcre-8.32.ziptobicher/root/boto_aws/pcre-8.32.zip

0bytestransferred/2030600bytestotal

507904bytestransferred/2030600bytestotal

1015808bytestransferred/2030600bytestotal

1523712bytestransferred/2030600bytestotal

2030600bytestransferred/2030600bytestotal

参数介绍,–bucket 后跟的是要上传的bucket名称,–multipart是支持多块上传,注意这个参数s3put默认是没有的,需要安装filechunkio 模块,用

pip install filechunkio 进行安装,安装完后就可以到这个参数选项了,–callback是回调函数,后面跟分几次进行上传,这个参数起到回显的功能,可以看到一个大文件分别上传到多少bytes,如果参数省略也可以上传成功,就是没有任何回显了。

2、第二种也是采用命令行方式,可以用s3cmd上传,因为这个命令我之前已经有详细介绍,今天就不在重复介绍了,可以在这里查看详细使用方法:

http://www.mindg.cn/?p=331

1

http://www.mindg.cn/?p=331

3、api方式,如果你觉得命令行方式用的不过瘾,就想用代码实现,也是可以的,其实aws针对大数据上次,也基本提供的是api接口模式,就是调用aws的api接口把文件分块上传到服务器上,然后服务器再把每个块组合成一个大的文件,这样的好处是如果传输中间网络断掉了,下次还可以根据传输的块编号进行续传,这就是基本原理,代码我是采用官方的例子,大家如果要用,可以根据自己的情况自行修改:

#!/usr/bin/env python

import math, os

import boto

from filechunkio import FileChunkIO

#Connect to S3

c = boto.connect_s3()

b = c.get_bucket('mybucket')

# Get file info

source_path = 'path/to/your/file.ext'

source_size = os.stat(source_path).st_size

#Create a multipart upload request

mp = b.initiate_multipart_upload(os.path.basename(source_path))

# Use a chunk size of 50 MiB (feel free to change this)

chunk_size = 52428800

chunk_count = int(math.ceil(source_size / float(chunk_size)))

# Send the file parts, using FileChunkIO to create a file-like object

# that points to a certain byte range within the original file. We

# set bytes to never exceed the original file size.

for i in range(chunk_count):

offset = chunk_size * i

bytes = min(chunk_size, source_size - offset)

with FileChunkIO(source_path, 'r', offset=offset, bytes=bytes) as fp:

mp.upload_part_from_file(fp, part_num=i + 1)

# Finish the upload

mp.complete_upload()

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

#!/usr/bin/env python

importmath,os

importboto

fromfilechunkioimportFileChunkIO

#Connect to S3

c=boto.connect_s3()

b=c.get_bucket('mybucket')

# Get file info

source_path='path/to/your/file.ext'

source_size=os.stat(source_path).st_size

#Create a multipart upload request

mp=b.initiate_multipart_upload(os.path.basename(source_path))

# Use a chunk size of 50 MiB (feel free to change this)

chunk_size=52428800

chunk_count=int(math.ceil(source_size/float(chunk_size)))

# Send the file parts, using FileChunkIO to create a file-like object

# that points to a certain byte range within the original file. We

# set bytes to never exceed the original file size.

foriinrange(chunk_count):

offset=chunk_size *i

bytes=min(chunk_size,source_size-offset)

withFileChunkIO(source_path,'r',offset=offset,bytes=bytes)asfp:

mp.upload_part_from_file(fp,part_num=i+1)

# Finish the upload

mp.complete_upload()

4、使用适用于分段上传的 REST API,官方介绍:

http://docs.aws.amazon.com/zh_cn/AmazonS3/latest/API/mpUploadInitiate.html

1

http://docs.aws.amazon.com/zh_cn/AmazonS3/latest/API/mpUploadInitiate.html

针对s3上传到文件的方式就写到这里里,这4种方式肯定沟通用了,各位看官可以根据自己业务实际情况进行选择,今天就到这里,如有问题,请各位看官斧正。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值