如何使用 Google 驱动器 API 上传文件?
How to upload files using the Google Drive API?
我正在尝试在 Android
上使用 Google 驱动器 api 上传文件
我在上面的 link 中注册了 SignInActivityWithDrive.java。
但是没有上传文件、下载文件的例子
我想知道如何上传和下载文件
谢谢
您可以在文档中找到 uploading and downloading 的基本示例。
正在上传
You can send upload requests in any of the following ways:
- Simple upload:
uploadType=media
. For quick transfer of a small file (5 MB or less). To perform a simple upload, refer to
Performing a Simple Upload.
- Multipart upload:
uploadType=multipart
. For quick transfer of a small file (5 MB or less) and metadata describing the file, all in
a single request. To perform a multipart upload, refer to
Performing a Multipart Upload.
- Resumable upload:
uploadType=resumable
. For more reliable transfer, especially important with large files. Resumable uploads
are a good choice for most applications, since they also work for
small files at the cost of one additional HTTP request per upload.
To perform a resumable upload, refer to Performing a Resumable
Upload.
以下示例展示了如何使用客户端库上传图像:
File fileMetadata = new File();
fileMetadata.setName("photo.jpg");
java.io.File filePath = new java.io.File("files/photo.jpg");
FileContent mediaContent = new FileContent("image/jpeg", filePath);
File file = driveService.files().create(fileMetadata, mediaContent)
.setFields("id")
.execute();
System.out.println("File ID: " + file.getId());
正在下载
Depending on the type of download you'd like to perform — a file, a
Google Document, or a content link — you'll use one of the following
URLs:
- Download a file — files.get with
alt=media
file resource
- Download and export a Google Doc — files.export
- Link a user to a file —
webContentLink
from the file resource
基本下载的示例是:
String fileId = "0BwwA4oUTeiV1UVNwOHItT0xfa2M";
OutputStream outputStream = new ByteArrayOutputStream();
driveService.files().get(fileId)
.executeMediaAndDownloadTo(outputStream);
现在是 2022 年,Google 驱动器 API 的工作方式可能已经发生了重大变化。我需要从具有终端访问权限的远程服务器上传大量大文件。这就是我让它为我工作的方式:
- 使用 this link 中详述的步骤创建 Google 服务 API(在您的本地计算机上)并获取 API 凭据。在 步骤 3 之前需要一个额外的步骤,转到左侧面板上的 'OAuth consent screen' 选项卡并完成所需的必要步骤。您只需执行一次。对于免费 google 帐户,您必须 select 外部作为 API 类型(但您始终可以将 api 保留在 testing 模式不允许其他人使用它)。还要在此面板中添加您希望用作测试用户的 gmail 地址。继续上述 link.
中的其余步骤
- 从第 1 步开始,您应该得到一个
client_secret_XXXXX.json
文件。使用 SCP 将其复制到您的远程计算机工作目录。将文件重命名为 client_secrets.json
.
pip install pydrive
- 在远程工作目录中导入并运行以下内容。
from pydrive.auth import GoogleAuth
gauth = GoogleAuth()
gauth.CommandLineAuth()
它将为您提供一个 link,您可以使用它从本地计算机登录到您的 google 帐户。您将获得一个登录密钥,您可以将其粘贴到您的远程终端中。
- 上传文件名列表
from pydrive.drive import GoogleDrive
drive = GoogleDrive(gauth)
for filename in filename_list:
## Enter folder ID here.
## You can get the folder Id from your drive link e.g.,
## https://drive.google.com/drive/u/2/folders/1pzschX3uMbxU0lB5WZ6IlEEeAUE8MZ-t
gfile = drive.CreateFile({'parents': [{'id': '1pzschX3uMbxU0lB5WZ6IlEEeAUE8MZ-t'}]})
gfile.SetContentFile(filename)
gfile.Upload() # Upload the file.
我正在尝试在 Android
上使用 Google 驱动器 api 上传文件我在上面的 link 中注册了 SignInActivityWithDrive.java。
但是没有上传文件、下载文件的例子
我想知道如何上传和下载文件
谢谢
您可以在文档中找到 uploading and downloading 的基本示例。
正在上传
You can send upload requests in any of the following ways:
- Simple upload:
uploadType=media
. For quick transfer of a small file (5 MB or less). To perform a simple upload, refer to Performing a Simple Upload.- Multipart upload:
uploadType=multipart
. For quick transfer of a small file (5 MB or less) and metadata describing the file, all in a single request. To perform a multipart upload, refer to Performing a Multipart Upload.- Resumable upload:
uploadType=resumable
. For more reliable transfer, especially important with large files. Resumable uploads are a good choice for most applications, since they also work for small files at the cost of one additional HTTP request per upload. To perform a resumable upload, refer to Performing a Resumable Upload.
以下示例展示了如何使用客户端库上传图像:
File fileMetadata = new File();
fileMetadata.setName("photo.jpg");
java.io.File filePath = new java.io.File("files/photo.jpg");
FileContent mediaContent = new FileContent("image/jpeg", filePath);
File file = driveService.files().create(fileMetadata, mediaContent)
.setFields("id")
.execute();
System.out.println("File ID: " + file.getId());
正在下载
Depending on the type of download you'd like to perform — a file, a Google Document, or a content link — you'll use one of the following URLs:
- Download a file — files.get with
alt=media
file resource- Download and export a Google Doc — files.export
- Link a user to a file —
webContentLink
from the file resource
基本下载的示例是:
String fileId = "0BwwA4oUTeiV1UVNwOHItT0xfa2M";
OutputStream outputStream = new ByteArrayOutputStream();
driveService.files().get(fileId)
.executeMediaAndDownloadTo(outputStream);
现在是 2022 年,Google 驱动器 API 的工作方式可能已经发生了重大变化。我需要从具有终端访问权限的远程服务器上传大量大文件。这就是我让它为我工作的方式:
- 使用 this link 中详述的步骤创建 Google 服务 API(在您的本地计算机上)并获取 API 凭据。在 步骤 3 之前需要一个额外的步骤,转到左侧面板上的 'OAuth consent screen' 选项卡并完成所需的必要步骤。您只需执行一次。对于免费 google 帐户,您必须 select 外部作为 API 类型(但您始终可以将 api 保留在 testing 模式不允许其他人使用它)。还要在此面板中添加您希望用作测试用户的 gmail 地址。继续上述 link. 中的其余步骤
- 从第 1 步开始,您应该得到一个
client_secret_XXXXX.json
文件。使用 SCP 将其复制到您的远程计算机工作目录。将文件重命名为client_secrets.json
. pip install pydrive
- 在远程工作目录中导入并运行以下内容。
from pydrive.auth import GoogleAuth
gauth = GoogleAuth()
gauth.CommandLineAuth()
它将为您提供一个 link,您可以使用它从本地计算机登录到您的 google 帐户。您将获得一个登录密钥,您可以将其粘贴到您的远程终端中。
- 上传文件名列表
from pydrive.drive import GoogleDrive
drive = GoogleDrive(gauth)
for filename in filename_list:
## Enter folder ID here.
## You can get the folder Id from your drive link e.g.,
## https://drive.google.com/drive/u/2/folders/1pzschX3uMbxU0lB5WZ6IlEEeAUE8MZ-t
gfile = drive.CreateFile({'parents': [{'id': '1pzschX3uMbxU0lB5WZ6IlEEeAUE8MZ-t'}]})
gfile.SetContentFile(filename)
gfile.Upload() # Upload the file.