将文件上传到gs桶中以进行循环

问题描述 投票:0回答:1

在下面的代码中,一个pdf文档被分割并保存在我的本地驱动器中,一旦分割过程完成,上传过程就会开始。在上传过程中,所有拆分的文件都将递归上传到gs存储桶。如何转换下面的代码,将分割后的文件直接上传到gs存储桶,而不是存储在本地然后上传?我尝试过但无法成功

#!/usr/bin/python3
import PyPDF2
from PyPDF2 import PdfFileWriter, PdfFileReader
import os
import glob
import sys
from google.cloud import storage

inputpdf = PdfFileReader(open(r"ace.pdf", "rb"))

for i in range(inputpdf.numPages):
    output = PdfFileWriter()
    output.addPage(inputpdf.getPage(i))
    with open(r"/home/playground/doc_pages/document-page%s.pdf" % i, "wb") as outputStream:
        output.write(outputStream)

def upload_local_directory_to_gcs(local_path, bucket, gcs_path):
        assert os.path.isdir(local_path)
        for local_file in glob.glob(local_path + '/**'):
            if not os.path.isfile(local_file):
                continue
            remote_path = os.path.join(gcs_path, local_file[1 + len(local_path) :])
            storage_client = storage.Client()
            buck = storage_client.bucket(bucket)
            blob = buck.blob(remote_path)
            blob.upload_from_filename(local_file)
            print("Uploaded " + local_file + " to gs bucket " + bucket)

upload_local_directory_to_gcs('/home/playground/doc_pages', 'doc_pages', '')
python google-cloud-platform google-cloud-storage pypdf2
1个回答
0
投票
#!/usr/bin/python3 import PyPDF2 from PyPDF2 import PdfFileWriter, PdfFileReader import os import glob import sys from google.cloud import storage inputpdf = PdfFileReader(open(r"ace.pdf", "rb")) # create temporal folder os.makedirs('/tmp/doc_pages') for i in range(inputpdf.numPages): output = PdfFileWriter() output.addPage(inputpdf.getPage(i)) # Write to temporal files with open(r"/tmp/doc_pages/document-page%s.pdf" % i, "wb") as outputStream: output.write(outputStream) def upload_local_directory_to_gcs(local_path, bucket, gcs_path): assert os.path.isdir(local_path) for local_file in glob.glob(local_path + '/**'): if not os.path.isfile(local_file): continue remote_path = os.path.join(gcs_path, local_file[1 + len(local_path) :]) storage_client = storage.Client() buck = storage_client.bucket(bucket) blob = buck.blob(remote_path) blob.upload_from_filename(local_file) print("Uploaded " + local_file + " to gs bucket " + bucket) upload_local_directory_to_gcs('/tmp/doc_pages', 'doc_pages', '') # Change source
© www.soinside.com 2019 - 2024. All rights reserved.