python - GAE - 处理外部 URI

标签 python google-app-engine pdf

我正在尝试在 GAE flexible 上处理 URI,特别是我正在通过 pdf2image 处理 pdf 文件。 每当我在 pdf2image 的 convert_from_path 上使用 URI 时,GAE 都会抛出

File not Found

,但如果我在我的本地机器上执行相同的过程,它会无误地执行。 我是否应该在 Google App Engine 上进行一些设置以允许它?

最佳答案

这个 pdf 在哪里?

您的标题说“外部 URI”,但 pdf2imageconvert_from_path 文档似乎表明该文件位于您的代码中。

如果该文件确实位于您的项目代码中并与您的项目一起部署,请尝试使用它来将相对路径转换为绝对路径:

curr_dir = os.path.dirname(os.path.realpath(__file__))
images = convert_from_path(curr_dir+'/my/relative/path/example.pdf')

编辑:

对于 GCS 上的 pdf,我会单独处理从 gcs 的下载,然后使用 convert_from_bytes 而不是 convert_from_path

您可以像这样设置与 GCS 的连接:

https://cloud.google.com/appengine/docs/flexible/python/using-cloud-storage

使用此函数获取 GCS blob:

https://googlecloudplatform.github.io/google-cloud-python/latest/storage/buckets.html#google.cloud.storage.bucket.Bucket.get_blob

然后使用这个函数实际下载字节:

https://googlecloudplatform.github.io/google-cloud-python/latest/storage/blobs.html#google.cloud.storage.blob.Blob.download_as_string

所以像这样:

client = storage.Client()
bucket = client.get_bucket('my-bucket')
blob = bucket.get_blob('/path/to/blob.pdf')
bytes = blob.download_as_string()
images = convert_from_bytes(bytes)

关于python - GAE - 处理外部 URI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51545321/

相关文章:

coding-style - 在 Python 中创建常量的约定

python - Python 中的异常与错误

python - NotImplementedError : Only tempfile. 临时文件可供使用

python - 将 SVG 转换为 PDF(svglib + reportlab 不够好)

python - 在文件中查找 == x 的行,而不是仅包含 x

python - 如何在 emacs 中使用 elpy 正确设置 Jedi?

node.js - Google App Engine 标准 Node JS 如何运行构建脚本?

google-app-engine - 您可以使用 Google App Engine 从其他地方托管的应用程序发送电子邮件吗?

php - 将 Contact form 7 中的表单结果导出为 PDF (fPDF)

c# - 对 PDF 文件进行数字签名