【问题标题】:Google Blobstore to Google DriveGoogle Blobstore 到 Google 云端硬盘
【发布时间】:2014-01-28 04:57:54
【问题描述】:

我花了几天时间研究,但我似乎找不到这个。有没有办法上传已经上传到 Google 的 blobstore 的 blob 并将其上传到我的驱动器。我可以创建新文档,但不能将现有文件从 blobstore 上传到用户的驱动器。我无法在 Google App Engine 环境中使用 FileOutputStream,而且似乎也无法使用 blobstore 的上传 url。

【问题讨论】:

    标签: google-app-engine google-drive-api blobstore


    【解决方案1】:

    我假设您拥有 OAuth2 访问令牌。如果是这种情况,类似下面的东西应该可以完成这项工作。它需要将 blob 文件读入字节数组。我还没有找到进行流式传输的方法,因此如果文件很大,您将不得不先将 blob 写入磁盘驱动器。

    import com.google.api.client.http.ByteArrayContent;
    import com.google.api.services.drive.Drive;
    import com.google.api.services.drive.model.File;
    import com.google.api.services.drive.model.ParentReference;
    import com.google.appengine.api.blobstore.BlobKey;
    import com.google.appengine.api.blobstore.BlobstoreService;
    import com.google.appengine.api.blobstore.BlobstoreServiceFactory;
    
    // creates the credential object from the OAuth2 Access Token
    GoogleCredential credential = new GoogleCredential.Builder()
        .setTransport(TRANSPORT).setJsonFactory(JSON_FACTORY)
        .setClientSecrets(clientSecrets.getWeb().getClientId(), clientSecrets.getWeb().getClientSecret())
        .build();
    credential.setAccessToken(accessToken);
    
    Drive drive = Drive.Builder(TRANSPORT, JSON_FACTORY, credential)
            .setApplicationName(APPLICATION_NAME).build();
    BlobstoreService blobstoreService = BlobstoreServiceFactory.getBlobstoreService();
    
    
    // reads the blobstore file into a byte array
    BlobKey blobKey = new BlobKey(blobKeyStr);
    byte[] blobBytes = blobstoreService.fetchData(blobKey, 0, BlobstoreService.MAX_BLOB_FETCH_SIZE-1);
    
    // creates the google drive file
    String parentId = ...; // find the parent reference for the google drive file
    File file = new File();
    file.setMimeType(mimeType);
    file.setTitle(...);
    file.setParents(Arrays.asList(new ParentReference().setId(parentId)));
    drive.files().insert(file, new ByteArrayContent(mimeType, blobBytes)).execute();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多