我正在尝试从一个 API 源下载并立即直接上传到 Google Drive API。我在下面附加的代码详细介绍了本地下载然后本地上传,如果可以的话,我想避免这种情况。我总是可以删除临时创建的本地文件,但这也是我想避免的。
from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
import json
import pandas as pd
from woocommerce import API
gauth = GoogleAuth()
drive = GoogleDrive(gauth)
wcapi = API(
url="https://my_website.com",
consumer_key="my_key",
consumer_secret="my_secret",
version="wc/v3"
)
products = wcapi.get("products", params={"per_page": 100, "page": 1}).json()
pd.json_normalize(products)
with open('products.json', 'w') as f:
json.dump(products, f)
f.close()
upload_file = 'products.json'
gfile = drive.CreateFile({'title': 'products.json',
'parents': [{'id': 'my_specific_folder'}]})
gfile.SetContentFile(upload_file)
gfile.Upload()