gpt4 book ai didi

python - 如何为 Google Cloud Storage 中存储的图像创建 PDF?

转载 作者:行者123 更新时间:2023-12-01 02:23:18 25 4
gpt4 key购买 nike

如果这是一个愚蠢的问题,我很抱歉。我对 GCP 还很陌生。

对于网络应用,我需要根据存储在 Cloud Storage 中的图像创建 PDF。

首先,我尝试将 python 包 fpdf 与存储在 Cloud Storage 中的文件一起使用,看看这是否可行。由于图像在线存储,因此我使用 urllib2 来获取图像。

代码:

from fpdf import FPDF
import urllib2
import os

imagelist = ["https://storage.googleapis.com/seventh-terrain-179700.appspot.com/excuses.jpg", "https://storage.googleapis.com/seventh-terrain-179700.appspot.com/excuses2.jpg"]

pdf = FPDF()
i = 0
for image in imagelist:
image = urllib2.urlopen(image)

# writing image files in current folder
with open('image'+str(i)+'.jpg','wb') as output:
output.write(image.read())

pdf.add_page()
pdf.image('image'+str(i)+'.jpg', 10, 10, 100, 100) # pdf.image(image,x,y,w,h)

# removing images
os.remove('image'+str(i)+'.jpg')
i += 1

# Creating PDF in current folder
pdf.output("yourfile.pdf", "F")

这句话很好。

然后我尝试在本地服务器中部署相同的代码:

import webapp2
from fpdf import FPDF
import urllib2
import os

pdf = FPDF()

class MainPage(webapp2.RequestHandler):
def get(self):
imagelist = ["https://storage.googleapis.com/seventh-terrain-179700.appspot.com/excuses.jpg", "https://storage.googleapis.com/seventh-terrain-179700.appspot.com/excuses2.jpg"]

pdf = FPDF()
i = 0
for image in imagelist:
image = urllib2.urlopen(image)

with open('image'+str(i)+'.jpg','wb') as output:
output.write(image.read())

pdf.add_page()
pdf.image('image'+str(i)+'.jpg', 10, 10, 100, 100) # pdf.image(image,x,y,w,h)

os.remove('image'+str(i)+'.jpg')
i += 1

pdf.output("yourfile.pdf", "F")

application = webapp2.WSGIApplication([('/', MainPage)],
debug=True)

但是,我收到错误:

WARNING  2017-12-08 19:21:56,184 sandbox.py:1082] The module _winreg is whitelisted for local dev only. If your application relies on _winreg, it is likely that it will not function properly in production.
WARNING 2017-12-08 14:21:56,190 urlfetch_stub.py:551] Stripped prohibited headers from URLFetch request: ['Host']
ERROR 2017-12-08 19:21:57,332 webapp2.py:1528] [Errno 30] Read-only file system: 'image0.jpg'
Traceback (most recent call last):
File "C:\Program Files (x86)\Google\Cloud SDK\google-cloud-sdk\platform\google_appengine\lib\webapp2-2.3\webapp2.py", line 1511, in __call__
rv = self.handle_exception(request, response, e)
File "C:\Program Files (x86)\Google\Cloud SDK\google-cloud-sdk\platform\google_appengine\lib\webapp2-2.3\webapp2.py", line 1505, in __call__
rv = self.router.dispatch(request, response)
File "C:\Program Files (x86)\Google\Cloud SDK\google-cloud-sdk\platform\google_appengine\lib\webapp2-2.3\webapp2.py", line 1253, in default_dispatcher
return route.handler_adapter(request, response)
File "C:\Program Files (x86)\Google\Cloud SDK\google-cloud-sdk\platform\google_appengine\lib\webapp2-2.3\webapp2.py", line 1077, in __call__
return handler.dispatch()
File "C:\Program Files (x86)\Google\Cloud SDK\google-cloud-sdk\platform\google_appengine\lib\webapp2-2.3\webapp2.py", line 547, in dispatch
return self.handle_exception(e, self.app.debug)
File "C:\Program Files (x86)\Google\Cloud SDK\google-cloud-sdk\platform\google_appengine\lib\webapp2-2.3\webapp2.py", line 545, in dispatch
return method(*args, **kwargs)
File "C:\MyMiniGCPProjects\FPDF\main.py", line 23, in get
with open('image'+str(i)+'.jpg','wb') as output:
File "C:\Program Files (x86)\Google\Cloud SDK\google-cloud-sdk\platform\google_appengine\google\appengine\tools\devappserver2\python\runtime\stubs.py", line 278, in __init__
raise IOError(errno.EROFS, 'Read-only file system', filename)
IOError: [Errno 30] Read-only file system: 'image0.jpg'
ERROR 2017-12-08 19:21:57,339 wsgi.py:279]
Traceback (most recent call last):
File "C:\Program Files (x86)\Google\Cloud SDK\google-cloud-sdk\platform\google_appengine\google\appengine\runtime\wsgi.py", line 267, in Handle
result = handler(dict(self._environ), self._StartResponse)
File "C:\Program Files (x86)\Google\Cloud SDK\google-cloud-sdk\platform\google_appengine\lib\webapp2-2.3\webapp2.py", line 1519, in __call__
response = self._internal_error(e)
File "C:\Program Files (x86)\Google\Cloud SDK\google-cloud-sdk\platform\google_appengine\lib\webapp2-2.3\webapp2.py", line 1511, in __call__
rv = self.handle_exception(request, response, e)
File "C:\Program Files (x86)\Google\Cloud SDK\google-cloud-sdk\platform\google_appengine\lib\webapp2-2.3\webapp2.py", line 1505, in __call__
rv = self.router.dispatch(request, response)
File "C:\Program Files (x86)\Google\Cloud SDK\google-cloud-sdk\platform\google_appengine\lib\webapp2-2.3\webapp2.py", line 1253, in default_dispatcher
return route.handler_adapter(request, response)
File "C:\Program Files (x86)\Google\Cloud SDK\google-cloud-sdk\platform\google_appengine\lib\webapp2-2.3\webapp2.py", line 1077, in __call__
return handler.dispatch()
File "C:\Program Files (x86)\Google\Cloud SDK\google-cloud-sdk\platform\google_appengine\lib\webapp2-2.3\webapp2.py", line 547, in dispatch
return self.handle_exception(e, self.app.debug)
File "C:\Program Files (x86)\Google\Cloud SDK\google-cloud-sdk\platform\google_appengine\lib\webapp2-2.3\webapp2.py", line 545, in dispatch
return method(*args, **kwargs)
File "C:\MyMiniGCPProjects\FPDF\main.py", line 23, in get
with open('image'+str(i)+'.jpg','wb') as output:
File "C:\Program Files (x86)\Google\Cloud SDK\google-cloud-sdk\platform\google_appengine\google\appengine\tools\devappserver2\python\runtime\stubs.py", line 278, in __init__
raise IOError(errno.EROFS, 'Read-only file system', filename)
IOError: [Errno 30] Read-only file system: 'image0.jpg'

我找不到任何有效的解决方案。有没有办法直接使用云存储中的文件并将 PDF 保存在云存储中?

最佳答案

您遇到了沙盒限制之一。来自 The sandbox :

An App Engine application cannot:

  • write to the filesystem. Applications must use Cloud Datastore for storing persistent data. Reading from the filesystem is allowed, and all application files uploaded with the application are available.

嗯,有关数据存储的注释实际上具有误导性,有多种存储选项,最适合您的情况的是恕我直言,云存储(GCS)。

但是您无法使用常规的 open() 将文件写入 GCS,您需要使用 GCS 客户端库。您可以在此处找到示例:Write a CSV to store in Google Cloud Storage

关于python - 如何为 Google Cloud Storage 中存储的图像创建 PDF?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47721024/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com