我正在嘗試使用 boto3 將 google play 控制臺報告上傳到 s3。當我嘗試在回圈中列印資料幀時,下面的代碼運行良好,這意味著我成功獲取了我需要的檔案。
from io import StringIO # python3; python2: BytesIO
import boto3
import os
from google.cloud import storage
import pandas as pd
import io
jsonfile = os.path.join(
os.path.dirname(__file__), 'private_key.json')
os.environ["GOOGLE_APPLICATION_CREDENTIALS"]= jsonfile
# getting all file names from bucket
storage_client = storage.Client()
bucket_name = 'pubsite_prod_rev_1223445566778899'
bucket = storage_client.bucket(bucket_name)
#blob = bucket.blob(source_blob_name)
allblobs = storage_client.list_blobs(bucket_name)
# filtering out the files i need. for example: abc/123/game1/201801_channel.csv,abc/123/game1/202110_channel.csv
for blobfile in allblobs:
if "abc/123" in blobfile.name:
if "game1" in blobfile.name:
if "channel.csv" in blobfile.name:
#print(blobfile.name) # successfully getting all file names
source_blob_name = blobfile.name
blob = bucket.blob(source_blob_name)
data = blob.download_as_string()
df = pd.read_csv(io.BytesIO(data),encoding='utf-16')
print(df) # successfully printing dataframes for all of the files
#upload files to s3
bucket = 'the-knights-iaps-raw' # already created on S3
csv_buffer = StringIO()
df.to_csv(csv_buffer)
s3_resource = boto3.resource('s3', aws_access_key_id='JE4WNFJCN24JNJN13FC',aws_secret_access_key = 'jdsafjlhsafj34j32n4tj23nZ')
fileNamefors3 = source_blob_name.split("/", 2)
s3_resource.Object(bucket, "Acquisition/Buyers7d/StickmanAdventureGame/Channel/" fileNamefors3[2]).put(Body=csv_buffer.getvalue())
但是將所有這些資料幀上傳到 s3 會導致錯誤:
檔案“C:\Users\USER\PycharmProjects\Gamexis_gpc\cvcv.py”,第 28 行,在 blob = bucket.blob(source_blob_name) AttributeError: 'str' object has no attribute 'blob'
我不是 python 專業人士,但如果有人可以提供幫助,那就太好了。
uj5u.com熱心網友回復:
在這里,您正在創建存盤桶:
bucket = storage_client.bucket(bucket_name)
.. 但后來在 for 回圈中你覆寫了那個變數:
bucket = 'the-knights-iaps-raw'
為字串使用不同的變數名稱。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/378529.html
