2013-05-11 12 views
6

Questo è il codice che sto attualmente utilizzando per leggere i file da Google Drive nel blobstore del motore di Google App.Come leggere file di grandi dimensioni dall'unità google in gae blobstore

private BlobKey getBlobKey(File f, DriveObject driveObject) 
      throws IOException, MalformedURLException { 

     Drive service = ((GoogleDrive) driveObject).getService(); 

     byte[] buffer = new byte[(int) f.getFileSize().intValue()]; 
     GenericUrl url = new GenericUrl(f.getDownloadUrl()); 
     HttpResponse response = service.getRequestFactory() 
        .buildGetRequest(url).execute(); 

     InputStream is = response.getContent(); 

     FileService fileService = FileServiceFactory.getFileService(); 
     AppEngineFile file = null; 
     boolean lock = true; 
     try { 
      file = fileService.createNewBlobFile("application/zip"); 
      FileWriteChannel writeChannel = fileService.openWriteChannel(
         file, lock); 

      int len; 
      while ((len = is.read(buffer)) >= 0) { 
      ByteBuffer bb = ByteBuffer.wrap(buffer, 0, len); 
      writeChannel.write(bb); 
      } 
      writeChannel.closeFinally(); 

     } catch (IOException e) { 
       // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

      BlobKey bk = fileService.getBlobKey(file); 

     return bk; 
    } 

Questo funziona bene per file di piccole dimensioni, ma dà un errore se il file è più di ~ 25 MB. C'è un modo migliore per fare ciò che sto cercando di fare?

Questo è l'errore che vedo

Uncaught exception from servlet 
com.google.appengine.api.urlfetch.ResponseTooLargeException: The response from url https://doc-0o-bs-docs.googleusercontent.com/docs/securesc/97o44sjeam7f23b1flie9m3qqidhrago/97q4akk8bmpub24itqhclo1vakjusu84/1368259200000/14350098165097390874/14350098165097390874/0ByS2XCxCEzq-NUVuajAwWGVoR1U?h=16653014193614665626&e=download&gd=true was too large. 
    at com.google.appengine.api.urlfetch.URLFetchServiceImpl.convertApplicationException(URLFetchServiceImpl.java:132) 
    at com.google.appengine.api.urlfetch.URLFetchServiceImpl.fetch(URLFetchServiceImpl.java:43) 
    at com.google.apphosting.utils.security.urlfetch.URLFetchServiceStreamHandler$Connection.fetchResponse(URLFetchServiceStreamHandler.java:417) 
    at com.google.apphosting.utils.security.urlfetch.URLFetchServiceStreamHandler$Connection.getInputStream(URLFetchServiceStreamHandler.java:296) 
    at com.google.apphosting.utils.security.urlfetch.URLFetchServiceStreamHandler$Connection.getResponseCode(URLFetchServiceStreamHandler.java:149) 
    at com.google.api.client.http.javanet.NetHttpResponse.<init>(NetHttpResponse.java:37) 
    at com.google.api.client.http.javanet.NetHttpRequest.execute(NetHttpRequest.java:95) 
    at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:980) 

Vorrei aggiungere che

Drive service = ((GoogleDrive) driveObject).getService(); 

rendimenti

new Drive.Builder(httpTransport, jsonFactory, cred) 
      .build() 
+0

App Engine limita la dimensione dei corpi dei messaggi HTTP. – Linuxios

+0

Capisco che è quello che sta succedendo, ma non c'è modo di aggirare questo? È piuttosto importante poter leggere file di grandi dimensioni nel blobstore. Forse ho bisogno di leggerlo in più messaggi. – rvabdn

+0

Posso caricare i file che mi servono dal mio computer tramite un servizio di blobstores, ma questo non è buono in quanto devo essere in grado di farlo tramite l'interfaccia del drive. – rvabdn

risposta

3

è possibile scaricare il file da Drive in blocchi. Utilizzare l'intestazione Range nella richiesta di download come documentato nello Google Drive documentation.

Range: bytes=500-999 
+0

Grazie mille! Questo è esattamente quello che speravo. – rvabdn

+0

Nate che esiste ancora una quota e il numero di byte che è possibile scaricare. Attualmente sto ricevendo un'eccezione 'urlfetch.Fetch()' attorno al segno da 50 MB. (Nota che non sono un utente pagante. :-) Vedi https://developers.google.com/appengine/docs/quotas#UrlFetch –

0

Questo FilsService & FileWriteChannel api è stato deprecato in favore del client App Engine GCS.

Problemi correlati