Pythonを使用したGoogle Cloud Storage Crudの究極のガイド

Google Cloud Storage Crudを歩いてください。これらの方法を参照して適用して、Google Cloudストレージでアプリケーションをセットアップできます

ビデオ、画像などのファイルストレージは、使用している可能性のあるほとんどのブランドサーバーの大部分を占める可能性があります。さらに、クラウドストレージ機能とビジネスアプリケーションとの相互作用は、クラウドストレージが優れているかどうかを評価するための最も重要な項目の1つです。

GBあたりのGoogleクラウドストレージ価格は、同等の高価ではありません。また、アプリケーションとの統合機能は非常に柔軟でフレンドリーです。したがって、この作品では、Google Cloud Storage Crudを歩きます。この作品の終わりまでに、これらの方法を参照して適用して、Google Cloudストレージでアプリケーションをセットアップできます

目次:Pythonを使用したGoogleクラウドストレージCrudの究極のガイド

バケツを作成します

GCPのバケットは、クラウドにデータを保存できる基本的なコンテナです。クラウドに保存するオブジェクトは、バケツに含まれているか、保存されています。ディレクトリやフォルダーを使用するのと同じ方法でバケットを使用することはできません。作成プロセスはもう少し制限的です。バケットを削除することもできません。したがって、WebアプリのGCP、機械学習の目的などにデータを保存して封じ込めたい場合は、バケットを作成することが1つのステップです。次のようなサンプルコードは次のとおりです。

from google.cloud import storage

def create_bucket(bucket_name):

   credentials = service_account.Credentials.from_service_account_info(json dict)

   bucket = client.create_bucket(bucket_name, location='US-EAST1')

   return f"Bucket {bucket.name} created."

bucket_name = "Buyfromlo Bucket"    

create_bucket(bucket_name)

ファイルをアップロードします

以下は、Pythonスクリプトを使用してファイルをアップロードするとともに、バケット作成を続行するサンプルコードです。

Google.Cloudからインポートストレージから

def upload_file(bucket_name):

    credentials = service_account.Credentials.from_service_account_info(json dict)

    bucket = client.bucket(bucket_name)

    file_name = 'my-file.txt'

    blob = bucket.blob(file_name)

    with open(file_name, 'rb') as f:

        acontents = f.read()

    blob.upload_from_string(contents)

    return f'File {file_name} uploaded to {blob.public_url}'

 bucket_name = "handsoncloud-new-bucket"

 upload_file(bucket_name)

ファイルの名前を変更します

命名は、特に機械学習のような大量のデータセットを扱うときに、ファイルアクセスで最も重要な処理の1つです。次のように、次のサンプルコードは、pythonを使用してファイルに自動的に名前を付ける方法を示します

    bucket.rename_blob(blob, new_file_name)

ファイルを更新します

metadata = {'description': 'This file metadata is updated via HandsOnCloud Tutorial'}

blob.metadata = metadata

blob.patch()

print(f'Metadata for file {file_name} updated.')

ファイルを削除します

blob = bucket.blob(file_name)

blob.delete()

print(f'File {file_name} deleted.')

フォルダーを作成します

folder_name = 'Buyfromlo Images'

folder = bucket.blob(folder_name)

folder.upload_from_string('')

print(f'Folder {folder_name} created.')

フォルダーを削除します

GCP runs a pay-as-you-go model which implies it doesn’t have any upfront cost, and it can hugely facilitate business to reduce regular recurring fixed cost. Being said that, it’s a monthly charging model. For instance, if you have 5GB data contained always-on in the GCP, it still costs your recurring fees. Therefore, accordingly you need to know how to delete unuseful data for the purpose to avoid wasting dollars.

次のようなサンプルコードは次のとおりです。

folder_name = 'buyfromlo'

folder = bucket.blob(folder_name)

folder.delete()

print(f'Folder {folder_name} deleted.')

GCSフォルダーへのパブリックアクセスを有効にします

By default the new files and dataset created and stored on GCP is not open to public. That implies your app or your script can’t access the file. You must activate and enable public access. Fortunately the way is very straightforward and easy. Here is the code sample as follows:

from google.cloud import storage

from typing import List

def make_bucket_public(bucket_name: str, members: List[str] = ["allUsers"]):

     credentials = service_account.Credentials.from_service_account_info(json dict)

    bucket = client.bucket(bucket_name)

    policy = bucket.get_iam_policy(requested_policy_version=3)

    policy.bindings.append({"role": "roles/storage.objectViewer", "members": members})

    bucket.set_iam_policy(policy)

    return f"Bucket {bucket.name} is now publicly readable"

Google Cloudストレージでホストされているファイルを取得します

Google Cloudストレージに含まれるデータセットの取得に関する詳細については、次のような記事を参照してください。

https://www.easy2digital.com/automation/data/chapter-78-fetching-media-files-using-google-cloud-storage-and-python/

かさばる相互作用、画像バイトデータを含むGoogleクラウドストレージCrudの完全なPythonスクリプト。

If you are interested in Chapter 83 –Ultimate Guide to Google Cloud Storage CRUD Using Python, please subscribe to our newsletter by adding the message ‘Chapter 83+ Full scripts of Google Cloud Storage CRUD. We would send you the script when the up-to-date app script is live.

第83章を読んでください。FlaskBlueprintを使用したPythoneBアプリ構造を使用して、Google Cloud Storage Crudのセグメントガイドをお楽しみください。もしそうなら、以下にリストされていることの1つを行うことで私たちをサポートしてください。

  • Support and Donate to our channel through PayPal (paypal.me/Easy2digital)
  • Subscribe to my channel and turn on the notification bell Easy2Digital Youtube channel.
  • Follow and like my page Easy2Digital Facebook page
  • ソーシャルネットワークの記事をハッシュタグ#easy2digitalで共有する
  • Easy2Digitalの最新の記事、ビデオ、および割引コードを受け取るために、毎週のニュースレターにサインアップしてください
  • Subscribe to our monthly membership through Patreon to enjoy exclusive benefits (www.patreon.com/louisludigital)