Resumable upload in Drive Rest API V3

前端 未结 4 1874
你的背包
你的背包 2020-12-05 16:00

I am trying to create a resumable upload session using drive rest API in Android.

As per the documentation the 3 steps needed to be followed are

  1. Start
4条回答
  •  半阙折子戏
    2020-12-05 16:37

    You don't have to care about all this logic. The documentation indeed explain the flow to complete a resumable upload but it's error prone if done "manually".
    Fortunately, Google expose a dedicated class to handle such case, i.e the MediaHttpUploader.

    This snippet of code do the job of a resumable upload on drive (same thing can be achieved on GCS):

    public class Main {
    
    private static final JacksonFactory JSON_FACTORY = new JacksonFactory();
    private static final NetHttpTransport HTTP_TRANSPORT = new NetHttpTransport();
    private static final MemoryDataStoreFactory DATA_STORE = new MemoryDataStoreFactory();
    
    public static void main(String... args) throws IOException {
    
    
        Credential credential = authorize();
    
        MediaHttpUploader mediaHttpUploader = new MediaHttpUploader(new FileContent("application/json", Paths.get("/path/to/foo.json").toFile()), HTTP_TRANSPORT, credential);
        mediaHttpUploader.setProgressListener(uploader -> System.out.println("progress: " + uploader.getProgress()));
        GenericUrl genericUrl = new GenericUrl(new URL("https://www.googleapis.com/upload/drive/v3/files?name=toto"));
        GenericJson data = new GenericJson();
        data.put("name", "title");
    
    
        JsonHttpContent jsonHttpContent = new JsonHttpContent(JSON_FACTORY, data);
        mediaHttpUploader.setMetadata(jsonHttpContent).upload(genericUrl);
    
        System.out.println("Finished");
    }
    
    private static Credential authorize() throws IOException {
        // load client secrets
        try (BufferedReader br = Files.newBufferedReader(Paths.get(Resources.getResource("client_secret.json").getPath()))) {
            GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY, br);
    
            // set up authorization code flow
            GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(
                    HTTP_TRANSPORT, JSON_FACTORY, clientSecrets,
                    Collections.singleton(DriveScopes.DRIVE))
                    .setAccessType("offline")
                    .setDataStoreFactory(DATA_STORE).build();
            // authorize
            return new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver()).authorize("user");
    
        }
    }
    

    }

    Note that no where we mention location. All the logic is hidden in the MediaHttpUploader class.
    So I don't really answer the question (where to find "Location") but I point the fact that this not really needed when using classes from Google library (and I am pretty sure other third party libraries exist to do the same job).

    UPDATE: the mediaHttpUploader is what is used under the hood by the Drive v3 client. So we can think about something like that:

          File fileMetadata = new File();
        fileMetadata.setName(UPLOAD_FILE.getName());
    
        FileContent mediaContent = new FileContent("image/jpeg", UPLOAD_FILE);
    
        Drive.Files.Create insert = drive.files().create(fileMetadata, mediaContent);
        MediaHttpUploader uploader = insert.getMediaHttpUploader();
        uploader.setDirectUploadEnabled(false);
        uploader.setProgressListener(new FileUploadProgressListener());
        return insert.execute();
    

提交回复
热议问题