繁体   English   中英

将枕头图像从PDF保存到Google Cloud Server

[英]Saving Pillow Images from PDF to Google Cloud Server

我正在开发一个Django Web应用程序,它接收PDF文件并对PDF的每个页面执行一些图像处理。 我收到PDF,我需要将每个页面保存到我的Google云端存储中。 我正在使用pdf2imageconvert_from_path()来生成PDF中每个页面的Pillow图像列表。 现在,我想将这些图像保存到Google云端存储中,但我无法弄明白。

我已经在本地成功保存了这些Pillow图像,但我不知道如何在云中执行此操作。

fullURL = file.pdf.url
client = storage.Client()
bucket = client.get_bucket('name-of-my-bucket')
blob = bucket.blob(file.pdf.name[:-4] + '/')
blob.upload_from_string('', content_type='application/x-www-form-urlencoded;charset=UTF-8')
pages = convert_from_path(fullURL, 400)
for i,page in enumerate(pages):
    blob = bucket.blob(file.pdf.name[:-4] + '/' + str(i) + '.jpg')
    blob.upload_from_string('', content_type='image/jpeg')
    outfile = file.pdf.name[:-4] + '/' + str(i) + '.jpg'
    page.save(outfile)
    of = open(outfile, 'rb')
    blob.upload_from_file(of)

由于您已在本地保存文件,因此它们可在运行Web应用程序的本地目录中使用。

您可以做的只是遍历该目录的文件并逐个上传到Google云端存储。

这是一个示例代码:

你需要这个库:

谷歌云存储

Python代码:

#Libraries
import os
from google.cloud import storage

#Public variable declarations:
bucket_name = "[BUCKET_NAME]"
local_directory = "local/directory/of/the/files/for/uploading/"
bucket_directory = "uploaded/files/" #Where the files will be uploaded in the bucket

#Upload file from source to destination
def upload_blob(source_file_name, destination_blob_name):
    storage_client = storage.Client()
    bucket = storage_client.get_bucket(bucket_name)
    blob = bucket.blob(destination_blob_name)

    blob.upload_from_filename(source_file_name)

#Iterate through all files in that directory and upload one by one using the same filename
def upload_files():
    for filename in os.listdir(local_directory):
        upload_blob(local_directory + filename, bucket_directory + filename)
    return "File uploaded!"

#Call this function in your code:
upload_files()

注意:我已经在Google App Engine网络应用中测试了代码,它对我有用。 了解它的工作原理并根据您的需要进行修改。 我希望这很有帮助。

所以从不使用blobstore开始。 他们正试图摆脱它并让人们使用云存储。 首先设置云存储

https://cloud.google.com/appengine/docs/standard/python/googlecloudstorageclient/setting-up-cloud-storage

我使用webapp2而不是Django,但我相信你可以搞清楚。 此外,我不使用枕头图像,因此您必须打开要上传的图像。 然后做这样的事情(这假设你试图发布数据):

  import cloudstorage as gcs
  import io
  import StringIO 
  from google.appengine.api import app_identity

在获取和发布自己的部分之前

     def create_file(self, filename, Dacontents):

    write_retry_params = gcs.RetryParams(backoff_factor=1.1)
    gcs_file = gcs.open(filename,
                        'w',
                        content_type='image/jpeg',
                        options={'x-goog-meta-foo': 'foo',
                                'x-goog-meta-bar': 'bar'},
                        retry_params=write_retry_params)
    gcs_file.write(Dacontents)
    gcs_file.close()

进入你的HTML

   <form action="/(whatever yoururl is)" method="post"enctype="multipart/form-data">
  <input type="file" name="orders"/>
   <input type="submit"/>
    </form>

在邮报中

    orders=self.request.POST.get(‘orders)#this is for webapp2

    bucket_name = os.environ.get('BUCKET_NAME',app_identity.get_default_gcs_bucket_name())
    bucket = '/' + bucket_name
    OpenOrders=orders.file.read()
    if OpenOrders:
        filename = bucket + '/whateverYouWantToCallIt'            
        self.create_file(filename,OpenOrders)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM