Le code suivant sera écrit et utilisé dans Jupyter Notebook.
On suppose que l'environnement a été créé et défini pour GCP et GCS.
Courir en premier.py
import os
import pandas as pd
from io import BytesIO
from pathlib import Path
from google.cloud import storage
from google.oauth2 import service_account
def get_project_credentials():
credentials = service_account.Credentials.from_service_account_file(
"xx/xx/xx/xx.json",#Le chemin où se trouve le fichier json pour la clé de compte de service
scopes=["https://www.googleapis.com/auth/cloud-platform"],
)
return credentials
def get_storage_client(credentials):
storage_client = storage.Client(
credentials = credentials,
project = credentials.project_id,
)
return storage_client
created_bucket.py
created_bucket_name = 'tmp_bucket_qitaqita'
credentials = get_project_credentials()
storage_client = get_storage_client(credentials)
bucket = storage_client.create_bucket(created_bucket_name)
Notez que les noms de bucket doivent être uniques dans le monde. (Au début, j'ai essayé de le créer avec le nom "tmp_bucket", mais j'ai eu une erreur car il existe déjà.)
trans_to_bucket.py
bucket_name = 'tmp_bucket_qitaqita'
file_name = 'send_txt.txt' #Nom de quoi envoyer
destination_blob_name = 'sended_txt.txt' #Nom à la destination
credentials = get_project_credentials()
storage_client = get_storage_client(credentials)
bucket = storage_client.get_bucket(bucket_name)
blob = bucket.blob(destination_blob_name)
blob.upload_from_filename(file_name)
Si vous nommez la destination comme suit, un répertoire appelé make_dir sera créé et les fichiers y seront transférés.
destination_blob_name = 'make_dir/sended_txt_2.txt'
del_bucket_file.py
bucket_name = 'tmp_bucket_qitaqita'
file_name = 'sended_txt.txt'#Le nom du fichier que vous souhaitez supprimer dans le bucket
credentials = get_project_credentials()
storage_client = get_storage_client(credentials)
bucket = storage_client.get_bucket(bucket_name)
blob = bucket.blob(file_name)
blob.delete()
bucket_name.py
credentials = get_project_credentials()
storage_client = get_storage_client(credentials)
blobs = storage_client.list_buckets()
for blob in blobs:
print(blob.name)
filename_in_bucket.py
bucket_name = 'tmp_bucket_qitaqita'
credentials = get_project_credentials()
storage_client = get_storage_client(credentials)
bucket = storage_client.get_bucket(bucket_name)
blobs = bucket.list_blobs()
for blob in blobs:
print(blob.name)
del_bucket.py
bucket_name = 'tmp_bucket_qitaqita'#Nom du bucket que vous souhaitez supprimer
credentials = get_project_credentials()
storage_client = get_storage_client(credentials)
bucket = storage_client.get_bucket(bucket_name)
bucket.delete()
Chargement de la sauvegarde.py
bucket_name = 'tmp_bucket_qitaqita'
file_name = 'sended_txt.txt'#Nom du fichier dans le bucket que vous souhaitez lire
credentials = get_project_credentials()
storage_client = get_storage_client(credentials)
bucket = storage_client.get_bucket(bucket_name)
blob = storage.Blob(file_name, bucket)
content = blob.download_as_string()
with open('sended_txt.txt', mode='wb') as f:
f.write(content)
Lire le fichier csv.py
bucket_name = 'tmp_bucket_qitaqita'
file_name = 'dataset_data.csv'
credentials = get_project_credentials()
storage_client = get_storage_client(credentials)
bucket = storage_client.get_bucket(bucket_name)
blob = storage.Blob(file_name, bucket)
content = blob.download_as_string()
df_csv = pd.read_csv(BytesIO(content))
A part: Ceci est le premier message à qiita. J'espère que cela aide.