Thursday, April 4, 2024
Cloud Storage Server GCP Juara GCP Python

App Dev: Storing Image and Video Files in Cloud Storage – Python

storing

“App Dev: Storing Image and Video Files in Cloud Storage – Python”

Pengantar

Cloud Storage memungkinkan penyimpanan dan pengambilan data dalam jumlah berapa pun kapan saja di seluruh dunia. Anda dapat menggunakan Cloud Storage untuk berbagai skenario termasuk menyajikan konten situs web, menyimpan data untuk pengarsipan dan pemulihan bencana, atau mendistribusikan objek data berukuran besar ke pengguna melalui unduhan langsung.

Praktikum

Task 1. Prepare the quiz application

Clone source code in Cloud Shell

git clone https://github.com/GoogleCloudPlatform/training-data-analyst

Configure and run the quiz application

cd ~/training-data-analyst/courses/developingapps/python/cloudstorage/start
. prepare_environment.sh
python run_server.py

Review the quiz application

  • To view the application, click Web preview > Preview on port 8080.
  • Click the Create Question link in the toolbar.

Task 2. Examine the quiz application code

Examine the application code

  • Navigate to the /training-data-analyst/courses/developingapps/python/cloudstorage/start folder using the file browser panel on the left side of the editor.
  • Select the add.html file in the ...quiz/webapp/templates/ folder.
  • Select the routes.py file in the ...quiz/webapp folder.
  • Select the questions.py file in the ...quiz/webapp folder.
  • Select the ...quiz/gcp/storage.py file.

Task 3. Create a Cloud Storage Bucket

  • Return to the Cloud Shell command line.
  • Stop the application by pressing Ctrl+C.
  • Create a Cloud Storage bucket named
gsutil mb gs://$DEVSHELL_PROJECT_ID-media
  • To export the Cloud Storage bucket name as an environment variable named GCLOUD_BUCKET, execute the following command:
export GCLOUD_BUCKET=$DEVSHELL_PROJECT_ID-media

Task 4. Adding objects to Cloud Storage

Import and use the Python Cloud Storage module

  • In code editor, move to the top of the ...quiz/gcp/storage.py file.
  • Get the bucket name from the GCLOUD_BUCKET environment variable.
  • Import the storage module from the google.client package.
  • Create a Cloud Storage client.
  • Get a reference to the Cloud Storage bucket.
Baca Juga :  Kubernetes Engine: Qwik Start

quiz/gcp/storage.py

# TODO: Get the Bucket name from the
# GCLOUD_BUCKET environment variable
bucket_name = os.getenv('GCLOUD_BUCKET')
# END TODO
# TODO: Import the storage module
from google.cloud import storage
# END TODO
# TODO: Create a client for Cloud Storage
storage_client = storage.Client()
# END TODO
# TODO: Use the client to get the Cloud Storage bucket
bucket = storage_client.get_bucket(bucket_name)
# END TODO

Write code to send a file to Cloud Storage

  • Still in storage.py, in the the upload_file(...) function, remove the existing pass statement, then use the Cloud Storage client to upload a file to your Cloud Storage bucket and make it publicly available.
  • Get a reference to a Cloud Storage blob object in the bucket.
  • Use the blob object to upload the image.
  • Make the file public.
  • Return the blob’s public URL.

quiz/gcp/storage.py – upload)file(...) function

"""
Uploads a file to a given Cloud Storage bucket and returns the public url
to the new object.
"""
def upload_file(image_file, public):
    # TODO: Use the bucket to get a blob object
    blob = bucket.blob(image_file.filename)
    # END TODO
    # TODO: Use the blob to upload the file
    blob.upload_from_string(
        image_file.read(),
        content_type=image_file.content_type)
    # END TODO
    # TODO: Make the object public
    if public:
        blob.make_public()
    # END TODO
    # TODO: Modify to return the blob's Public URL
    return blob.public_url
    # END TODO

Write code to use the Cloud Storage functionality

  • In the editor, move to the top of the ...quiz/webapp/questions.py file.
  • Modify the import statement to use your storage client as well as the datastore client.
  • Move to the upload_file(...) function. Use your storage client to upload a file, and assign the returned public URL to a variable.
  • Modify the return statement to return the public URL.
  • Move to the save_question(...) function. Write an if test to see if the image_file is present.
  • If it is, then call the upload_file(...) function, and assign the public URL to a entity property named imageUrl.
  • If not, then assign an empty string to the entity imageUrl property.
Baca Juga :  App Dev: Storing Application Data in Cloud Datastore - Python

quiz/webapp/questions.py

# TODO: Import the storage module
from quiz.gcp import storage, datastore
# END TODO
"""
uploads file into google cloud storage
- upload file
- return public_url
"""
def upload_file(image_file, public):
    if not image_file:
        return None
    # TODO: Use the storage client to Upload the file
    # The second argument is a boolean
    public_url = storage.upload_file(
       image_file,
       public
    )
    # END TODO
    # TODO: Return the public URL
    # for the object
    return public_url
    # END TODO
"""
uploads file into google cloud storage
- call method to upload file (public=true)
- call datastore helper method to save question
"""
def save_question(data, image_file):
    # TODO: If there is an image file, then upload it
    # And assign the result to a new Datastore
    # property imageUrl
    # If there isn't, assign an empty string
    if image_file:
        data['imageUrl'] = str(
                  upload_file(image_file, True))
    else:
        data['imageUrl'] = u''
    # END TODO
    data['correctAnswer'] = int(data['correctAnswer'])
    datastore.save_question(data)
    return

Run the application and create a Cloud Storage object

  • Save the ...gcp/storage.py and ...webapp/questions.py files, and then return to the Cloud Shell command.
  • Return to Cloud Shell to run the application:
python run_server.py
  • Download an image file to your local machine from Google storage.
  • In Cloud Shell, click Web preview > Preview on port 8080 to preview the Quiz application.
  • Click the Create Question link.
  • Complete the form with the following values, and then click Save.
  • Return to the Cloud Console and navigate to Navigation menu > Cloud Storage.
  • On the Cloud Storage > Browser page, click the correct bucket (named <Project ID>-media).

Run the client application and test the Cloud Storage public URL

  • Add /api/quizzes/gcp to the end of the application’s URL.
  • Return to the application home page and click the Take Test link.
  • Click GCP, and answer each question.
Baca Juga :  Berinteraksi dengan Modul Terraform GCP

Pentup

Sahabat Blog Learning & Doing demikianlah penjelasan mengenai App Dev: Storing Image and Video Files in Cloud Storage – Python. Semoga Bermanfaat . Sampai ketemu lagi di postingan berikut nya.

(Visited 93 times, 1 visits today)

Similar Posts