Joseph N
Joseph N

Reputation: 560

How to send FPDF output to GCS bucket from python?

I am using FPDF python library to create new pdf file. I want my output pdf to be stored on gcs bucket. My code is below:

import fpdf

data=[1,2,3,4,5,6]

pdf = fpdf.FPDF(format='letter')
pdf.add_page()
pdf.set_font("Arial", size=12)

for i in data:
    pdf.write(5,str(i))
    pdf.ln()
pdf.output("gs://bucket_name/output.pdf")

its giving No such file or directory: error.

How can i achieve this?

Upvotes: 1

Views: 639

Answers (1)

Andres S
Andres S

Reputation: 1247

You can upload the file from local storage to GCS using the below code example from the GCS docs

(Look for Code examples > python)

from google.cloud import storage


def upload_blob(bucket_name, source_file_name, destination_blob_name):
"""Uploads a file to the bucket."""
# bucket_name = "your-bucket-name"
# source_file_name = "local/path/to/file"
# destination_blob_name = "storage-object-name"

storage_client = storage.Client()
bucket = storage_client.bucket(bucket_name)
blob = bucket.blob(destination_blob_name)

blob.upload_from_filename(source_file_name)

print(
    "File {} uploaded to {}.".format(
        source_file_name, destination_blob_name
    )
)

Edit

Cloud Storage supports streaming transfers, which allow you to stream data to and from your Cloud Storage account without requiring that the data first be saved to a file.

But per the client libraries > Python note "You cannot currently perform streaming uploads with the Python client library."

Upvotes: 1

Related Questions