简体   繁体   中英

How to upload files using the Google Drive API?

I'm trying to upload a file using the Google Drive api on Android

https://github.com/googlesamples/google-services/tree/master/android/signin/app/src/main/java/com/google/samples/quickstart/signin

I signed up to SignInActivityWithDrive.java in the link above.

But there is no example of uploading a file, downloading a file

I want to know how to upload and download files

Thank you

You can find basic examples of uploading and downloading in the docs.

Uploading

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 .

The following example shows how to upload an image using the client libraries:

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());

Downloading

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:

An example of a basic download is:

String fileId = "0BwwA4oUTeiV1UVNwOHItT0xfa2M";
OutputStream outputStream = new ByteArrayOutputStream();
driveService.files().get(fileId)
    .executeMediaAndDownloadTo(outputStream);

It is 2022 now, and how the Google Drive API works might have changed significantly. I needed to upload a number of large files from a remote server where I have terminal access. This is how I got it working for me:

  1. Use the steps detailed in this link to create a Google Services API (on your local computer) and get the API credentials. An extra step was required before step 3 , go to the 'OAuth consent screen' tab on the panel to the left and complete necessary steps required. You have to do this only once. For free google accounts, you'll have to select External as the API type (but you can always keep the api in testing mode to not allow others to use it). Also add the gmail address you wish to use as a test user in this panel. Continue the rest of the steps from the aforementioned link.
  2. From Step 1 you should get a client_secret_XXXXX.json file. Copy it to your remote computer working directory using SCP. Rename the file to client_secrets.json .
  3. pip install pydrive
  4. Import and run the following inside the remote working directory.
from pydrive.auth import GoogleAuth
gauth = GoogleAuth()           
gauth.CommandLineAuth()

It will provide you a link that you can use to log into your google account from your local computer. You will get a login key that you will have paste into your remote terminal.

  1. Upload a list of filenames
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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM