首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将枕头图像从PDF保存到Google服务器

将枕头图像从PDF保存到Google服务器
EN

Stack Overflow用户
提问于 2019-04-02 21:24:14
回答 2查看 818关注 0票数 0

我正在开发一个Django web应用程序,它接收PDF文件,并对PDF的每个页面执行一些图像处理。我得到一个PDF,我需要将每个页面保存到我的。我正在使用pdf2imageconvert_from_path()为PDF中的每一页生成一个枕头图像列表。现在,我想把这些图像保存到Google存储库中,但是我想不出答案。

我已经成功地在本地保存了这些枕头图像,但是我不知道如何在云中实现这一点。

代码语言:javascript
复制
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)
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-04-04 05:43:43

所以从不使用by商店开始吧。他们试图摆脱它,让人们使用云存储。首先设置云存储

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

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

代码语言:javascript
复制
  import cloudstorage as gcs
  import io
  import StringIO 
  from google.appengine.api import app_identity

在get和post在它自己的部分之前

代码语言:javascript
复制
     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中

代码语言:javascript
复制
   <form action="/(whatever yoururl is)" method="post"enctype="multipart/form-data">
  <input type="file" name="orders"/>
   <input type="submit"/>
    </form>

在邮政

代码语言:javascript
复制
    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)
票数 0
EN

Stack Overflow用户

发布于 2019-04-03 09:52:27

因为您已经在本地保存了这些文件,那么它们就可以在运行web应用程序的本地目录中使用。

您可以做的只是迭代该目录的文件,然后一个一个地将它们上传到。

这里是一个示例代码:

您将需要这个库:

谷歌云存储

Python代码:

代码语言:javascript
复制
#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()

注意:我在应用程序中测试了代码,它对我起了作用。了解它是如何工作的,并根据您的需要对其进行修改。我希望这能帮上忙。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55483775

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档