簡體   English   中英

從亞馬遜下載和壓縮文件

[英]Downloading and zipping files from amazon

我目前正將所有照片存儲在亞馬遜s3上,並使用django作為我的網站。 我希望有一個按鈕,允許用戶點擊它並將所有照片壓縮並返回給他們。

我目前正在使用boto與amazon接口,發現我可以瀏覽整個存儲桶列表/使用get_key查找特定文件並下載它們

在此之后,我需要暫時存儲它們,然后拉鏈並返回。

這樣做的最佳方法是什么?

謝謝

使用這個拉取請求修補python-zipstream ,您可以執行以下操作:

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迭代關鍵內容,使用此答案中iterable_to_stream方法將此迭代器轉換為流,然后讓python-zipstream創建一個zip文件。

您可以查看此問題或在此片段下載該文件

# 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按照我給你聯系 ,你也可以使用的zipimport如這里的例子是在頁面的底部,按照該文件的新版本

您可能也有興趣在 ,雖然它被Django的1.2做,它可能無法在工作1.3

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM