Spaces:
Sleeping
Sleeping
with io.BytesIO(content.encode('utf-8')) as fh:
Browse files
app.py
CHANGED
@@ -106,23 +106,32 @@ def upload_content_directly(service, file_name, folder_id, content):
|
|
106 |
"""
|
107 |
直接将内容上传到Google Drive中的新文件。
|
108 |
"""
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
print(content)
|
116 |
-
print("==content==")
|
117 |
-
|
118 |
-
print("==media==")
|
119 |
-
print(media)
|
120 |
-
print("==media==")
|
121 |
-
|
122 |
|
123 |
-
|
124 |
-
|
125 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
126 |
|
127 |
def download_file_as_string(service, file_id):
|
128 |
"""
|
|
|
106 |
"""
|
107 |
直接将内容上传到Google Drive中的新文件。
|
108 |
"""
|
109 |
+
if not file_name:
|
110 |
+
raise ValueError("文件名不能为空")
|
111 |
+
if not folder_id:
|
112 |
+
raise ValueError("文件夹ID不能为空")
|
113 |
+
if content is None: # 允许空字符串上传,但不允许None
|
114 |
+
raise ValueError("内容不能为空")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
115 |
|
116 |
+
file_metadata = {'name': file_name, 'parents': [folder_id]}
|
117 |
+
# 使用io.BytesIO为文本内容创建一个内存中的文件对象
|
118 |
+
try:
|
119 |
+
with io.BytesIO(content.encode('utf-8')) as fh:
|
120 |
+
media = MediaIoBaseUpload(fh, mimetype='text/plain', resumable=True)
|
121 |
+
|
122 |
+
print("==content==")
|
123 |
+
print(content)
|
124 |
+
print("==content==")
|
125 |
+
|
126 |
+
print("==media==")
|
127 |
+
print(media)
|
128 |
+
print("==media==")
|
129 |
+
# 执行上传
|
130 |
+
file = service.files().create(body=file_metadata, media_body=media, fields='id').execute()
|
131 |
+
return file.get('id')
|
132 |
+
except Exception as e:
|
133 |
+
print(f"上传文件时发生错误: {e}")
|
134 |
+
raise # 重新抛出异常,调用者可以根据需要处理或忽略
|
135 |
|
136 |
def download_file_as_string(service, file_id):
|
137 |
"""
|