问题
I implemented this library for generate barcodes images (http://kennethngedo.wordpress.com/2014/02/07/how-to-generate-barcode-in-django-using-reportlab/)
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.
I'm using this Django structure (http://django-skel.readthedocs.org/en/latest/) specially adapted to work on Heroku with Amazon S3.
Do you know guys how can I upload the generated image on my Media folder on Amazon?
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.
Thanks!
回答1:
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)
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.
来源:https://stackoverflow.com/questions/24375868/save-image-generated-with-reportlab-in-my-media-folder-in-amazon-s3