将使用Reportlab生成的图像保存到我的MEDIA文件夹中(在Amazon S3中) [英] Save Image generated with Reportlab in my MEDIA folder (in Amazon S3)

查看:152
本文介绍了将使用Reportlab生成的图像保存到我的MEDIA文件夹中(在Amazon S3中)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我实现了此库以生成条形码图像( http://kennethngedo.wordpress.com/2014/02/07/how-to-generate-barcode-in-django-using-reportlab/)

I implemented this library for generate barcodes images (http://kennethngedo.wordpress.com/2014/02/07/how-to-generate-barcode-in-django-using-reportlab/)

一切正常,可以正确生成图像,但是...在项目外部的文件夹中创建了图像,例如,我将Heroku用于生产环境,因此无法访问该图像.

Everything works fine, the image is generated correctly, BUT... the image is created in a folder outside the project, and such I'm using Heroku for Production, I can't access to the image.

我正在使用这种Django结构( http://django-skel.readthedocs.org/en/latest/)专门用于在Amazon S3的Heroku上运行.

I'm using this Django structure (http://django-skel.readthedocs.org/en/latest/) specially adapted to work on Heroku with Amazon S3.

你们知道我如何在Amazon的Media文件夹上上传生成的图像?

Do you know guys how can I upload the generated image on my Media folder on Amazon?

这是我创建并保存图像的 Views.py :

This is my Views.py where the image is created and saved:

from random import randint
from reportlab.lib.units import mm
from reportlab.graphics.barcode import *
from reportlab.graphics.shapes import Drawing, String
from django.shortcuts import render_to_response


class MyBarcodeDrawing(Drawing):
    def __init__(self, text_value, *args, **kw):
        barcode = createBarcodeDrawing('Code128', value=text_value, barHeight=10*mm, humanReadable=True)
        Drawing.__init__(self,barcode.width,barcode.height,*args,**kw)
        self.add(barcode, name='barcode')


def barcode_generator(barcode_value):
    text = barcode_value
    filename = "nightology_barcode_" + barcode_value
    path_to_save = "media/barcodes/"
    b = MyBarcodeDrawing(text)
    b.save(formats=['gif','pdf'],outDir=path_to_save,fnRoot=filename)
    barcodePicUrl = "barcode/"+ filename + ".gif"
    return barcodePicUrl

我希望有人可以帮助我...我会很感激的.

I hope somebody could help me on this... I'll really appreciate.

谢谢!

推荐答案

我有一个类似的问题,但是没有Amazon S3部分.对我来说,在媒体文件夹中创建一个新文件真的很容易.我可以简单地使用default_storage来获取媒体文件夹的路径:

I had a similar Problem but without the Amazon S3 part. For me it was really easy to create a new File in the media folder. I could simply use default_storage to get the path to the media folder:

from django.core.files.storage import default_storage
import os

# Get the path to the barcodes folder in the media folder
# check if the folder exists and create it if not

folderPath = default_storage.path('barcodes')
if not default_storage.exists('barcodes'):
    os.mkdir(folderPath)

由于django-skel似乎正在为Amazon S3使用django-storage后端,因此它也适用于您的设置.如果Amazon S3存储后端不是默认存储后端,则可能必须直接使用 S3Storage 类.

Since django-skel seems to be using a django-storage backend for Amazon S3 this should also work for your setup. If the Amazon S3 storage backend is not the default storage backend, you might have to use the S3Storage class directly.

这篇关于将使用Reportlab生成的图像保存到我的MEDIA文件夹中(在Amazon S3中)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆