从亚马逊下载和压缩文件

时间:2012-02-29 09:23:37

标签: python django amazon-s3 boto

我目前正将所有照片存储在亚马逊s3上,并使用django作为我的网站。我希望有一个按钮,允许用户点击它并将所有照片压缩并返回给他们。

我目前正在使用boto与amazon接口,发现我可以查看整个存储桶列表/使用get_key查找特定文件并下载

在此之后,我需要暂时存储它们,然后拉链并返回。

这样做的最佳方式是什么?

由于

2 个答案:

答案 0 :(得分:1)

您可以查看此question或此snippet下载文件

# This is not a full working example, just a starting point
# for downloading images in different formats.

import subprocess
import Image

def image_as_png_pdf(request):
  output_format = request.GET.get('format')
  im = Image.open(path_to_image) # any Image object should work
  if output_format == 'png':
    response = HttpResponse(mimetype='image/png')
    response['Content-Disposition'] = 'attachment; filename=%s.png' % filename
    im.save(response, 'png') # will call response.write()
  else:
    # Temporary disk space, server process needs write access
    tmp_path = '/tmp/'
    # Full path to ImageMagick convert binary
    convert_bin = '/usr/bin/convert' 
    im.save(tmp_path+filename+'.png', 'png')
    response = HttpResponse(mimetype='application/pdf')
    response['Content-Disposition'] = 'attachment; filename=%s.pdf' % filename
    ret = subprocess.Popen([ convert_bin, 
                            "%s%s.png"%(tmp_path,filename), "pdf:-" ],
                            stdout=subprocess.PIPE)
    response.write(ret.stdout.read())
  return response

要创建一个zip跟随link that i gave you,您也可以使用zipimport,如下所示here示例位于页面底部,请按documentation获取更新版本

您可能也对this感兴趣,虽然它是为django 1.2制作的,但它可能不适用于1.3

答案 1 :(得分:1)

使用python-zipstream修补this pull request可以执行以下操作:

import boto
import io
import zipstream
import sys


def iterable_to_stream(iterable, buffer_size=io.DEFAULT_BUFFER_SIZE):
    """
    Lets you use an iterable (e.g. a generator) that yields bytestrings as a
    read-only input stream.

    The stream implements Python 3's newer I/O API (available in Python 2's io
    module).  For efficiency, the stream is buffered.

    From: https://stackoverflow.com/a/20260030/729491
    """
    class IterStream(io.RawIOBase):
        def __init__(self):
            self.leftover = None

        def readable(self):
            return True

        def readinto(self, b):
            try:
                l = len(b)  # We're supposed to return at most this much
                chunk = self.leftover or next(iterable)
                output, self.leftover = chunk[:l], chunk[l:]
                b[:len(output)] = output
                return len(output)
            except StopIteration:
                return 0    # indicate EOF
    return io.BufferedReader(IterStream(), buffer_size=buffer_size)


def iterate_key():
    b = boto.connect_s3().get_bucket('lastage')
    key = b.get_key('README.markdown')
    for b in key:
        yield b

with open('/tmp/foo.zip', 'w') as f:
    z = zipstream.ZipFile(mode='w')
    z.write(iterable_to_stream(iterate_key()), arcname='foo1')
    z.write(iterable_to_stream(iterate_key()), arcname='foo2')
    z.write(iterable_to_stream(iterate_key()), arcname='foo3')
    for chunk in z:
        print "CHUNK", len(chunk)
        f.write(chunk)

基本上我们使用boto迭代关键内容,使用this answer中的iterable_to_stream方法将此迭代器转换为流,然后让python-zipstream即时创建一个zip文件