2017-06-21 7 views
0

ダウンロードリンクからgoogleドライブにファイルをアップロードする方法をお探しです。Googleドライブへのリンクを含むファイルをアップロードする方法REST API v2

どのように行うのか説明しているドキュメントには何も見つかりませんでした。

EDIT:

私が見つかった二つの選択肢:

  • 最初の出力である
  • 第二のループからcreatedFileファイルのIDを回復するために最初の反復でありますその後、更新を行います。

最初の解決策は最も速いようです。 1つのクラウドから別のクラウドへのファイルのコピー/ペーストを実行すると、たとえばソリッドエクスプローラでははるかに高速になります。 よりよい解決策が存在する必要があります? `

@Override 
    protected String doInBackground(String... sUrl) { 
     File body = new File(); 
     body.setTitle("some name"); 
     String fileId = null; 
     File createdFile; 
     InputStream input = null; 
     ByteArrayOutputStream bos = null; 
     HttpURLConnection connection = null; 
     try { 
      URL url = new URL(sUrl[0]); 
      connection = (HttpURLConnection) url.openConnection(); 
      connection.connect(); 

      if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) { 
       return "Server returned HTTP " + connection.getResponseCode() 
         + " " + connection.getResponseMessage(); 
      } 

      // download the file 
      input = connection.getInputStream(); 
      bos = new ByteArrayOutputStream(); 

      byte data[] = new byte[4096]; 
      total = 0; 
      int count; 
      while ((count = input.read(data)) != -1) { 
       // allow canceling with back button 
       if (isCancelled()) { 
        input.close(); 
        return null; 
       } 
       total += count; 

       bos.write(data, 0, count); 
       //option 2 
       if (fileId == null){ 
        createdFile = mService.files().insert(body, new ByteArrayContent("", bos.toByteArray())).execute(); 
        fileId = createdFile.getId(); 

       }else{ 
        createdFile = mService.files().update(fileId, createdFile, new ByteArrayContent("", bos.toByteArray())).execute(); 
       } 

      } 
      //option 1 
      createdFile = mService.files().insert(body, new ByteArrayContent("", bos.toByteArray())).execute(); 
     } catch (Exception e) { 
      return e.toString(); 
     } finally { 
      try { 
       if (bos != null) 
        bos.close(); 
       if (input != null) 
        input.close(); 
      } catch (IOException ignored) { 
      } 

      if (connection != null) 
       connection.disconnect(); 
     } 
     return null; 
    } 
    .............. 

あなたは私の解決策を教えてください。

答えて

0

最後に、私はこのように進ん:

@Override 
    protected String doInBackground(String... sUrl) { 

     com.google.api.services.drive.model.File body = new com.google.api.services.drive.model.File(); 
     body.setTitle("some name"); 
     InputStream input = null; 
     InputStreamContent mediaContent = null; 
     HttpURLConnection connection = null; 
     try { 
      URL url = new URL(sUrl[0]); 
      connection = (HttpURLConnection) url.openConnection(); 
      if (cookies != null){ 
       connection.setRequestProperty("Cookie", cookies); 
       connection.setRequestProperty("User-Agent", agent); 
       connection.setRequestProperty("Accept", "text/html, application/xhtml+xml, *" + "/" + "*"); 
       connection.setRequestProperty("Accept-Language", "en-US,en;q=0.7,he;q=0.3"); 
       connection.setRequestProperty("Referer", sUrl[0]); 
      } 
      connection.connect(); 

      if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) { 
       return "Server returned HTTP " + connection.getResponseCode() 
         + " " + connection.getResponseMessage(); 
      } 

      input = connection.getInputStream(); 

      mediaContent = new InputStreamContent("", new BufferedInputStream(connection.getInputStream()) { 
       @Override 
       public int read() throws IOException { 

        return 0; 
       } 
       @Override 
       public int read(byte[] buffer, 
           int byteOffset, int byteCount) 
         throws IOException { 

        return super.read(buffer, byteOffset, byteCount); 
       } 
      }); 

      com.google.api.services.drive.model.File f = mService.files().insert(body, mediaContent).execute(); 

     } catch (Exception e) { 
      return e.toString(); 
     } finally { 
      try { 
       if (input != null) 
        input.close(); 
      } catch (IOException ignored) { 
      } 

      if (connection != null) 
       connection.disconnect(); 
     } 
     return null; 
    } 

私はこれが役立つことを願っています。

1

任意の一意のIDを設定することはできません。 2つのオプションがあります。

  • nullのままにしておきます。 最も一般的な仕組みはIDを一切設定しないことです。ファイルを作成すると、GoogleドライブはIDを作成してcreatedFile.idに戻ります。

  • IDのキャッシュを要求します。 https://developers.google.com/drive/v2/reference/files/generateIdsを使用していくつかのIDを取得し、使用できるように予約することができます。 body.idにこれらのIDのいずれかを設定できます。

  • しかし、実際の問題は、ファイルIDとファイル名の重要性をまだ理解していないと思われます。 GoogleドライブではファイルはIDで識別され、その名前はではありません。ファイル名は、変更された日付とMIMEタイプのように、単にファイルのプロパティです。 10を作成すると、10個のファイルが作成されます。それらのすべてが同じ名前を持っていれば、10種類のファイルのそれぞれが同じ名前になります。

    新しいファイルを作成するのではなく、同じファイルを更新する場合は、作成の代わりにhttps://developers.google.com/drive/v2/reference/files/updateを使用する必要があります。

    +0

    - 最初は、ループからcreatedFileを出力します - 2番目は最初の反復で、ファイルのIDを復元してから更新します。 最初の解決策が最も速いようです。 1つのクラウドから別のクラウドへのファイルのコピー/ペーストを実行すると、たとえばソリッドエクスプローラでははるかに高速になります。 もっと良い解決策が必要ですか? – Aristide13

    関連する問題