Java Code Examples for com.google.api.client.http.InputStreamContent#setLength()

The following examples show how to use com.google.api.client.http.InputStreamContent#setLength() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: StorageSample.java    From java-docs-samples with Apache License 2.0 6 votes vote down vote up
/**
 * Uploads data to an object in a bucket.
 *
 * @param name the name of the destination object.
 * @param contentType the MIME type of the data.
 * @param file the file to upload.
 * @param bucketName the name of the bucket to create the object in.
 */
public static void uploadFile(String name, String contentType, File file, String bucketName)
    throws IOException, GeneralSecurityException {
  InputStreamContent contentStream =
      new InputStreamContent(contentType, new FileInputStream(file));
  // Setting the length improves upload performance
  contentStream.setLength(file.length());
  StorageObject objectMetadata =
      new StorageObject()
          // Set the destination object name
          .setName(name)
          // Set the access control list to publicly read-only
          .setAcl(
              Arrays.asList(new ObjectAccessControl().setEntity("allUsers").setRole("READER")));

  // Do the insert
  Storage client = StorageFactory.getService();
  Storage.Objects.Insert insertRequest =
      client.objects().insert(bucketName, objectMetadata, contentStream);

  insertRequest.execute();
}
 
Example 2
Source File: MediaHttpUploaderTest.java    From google-api-java-client with Apache License 2.0 6 votes vote down vote up
public void testDirectMediaUpload_WithSpecifiedHeader() throws Exception {
  int contentLength = MediaHttpUploader.DEFAULT_CHUNK_SIZE * 2;
  MediaTransport fakeTransport = new MediaTransport(contentLength);
  fakeTransport.directUploadEnabled = true;
  fakeTransport.assertTestHeaders = true;
  InputStream is = new ByteArrayInputStream(new byte[contentLength]);
  InputStreamContent mediaContent = new InputStreamContent(TEST_CONTENT_TYPE, is);
  mediaContent.setLength(contentLength);
  MediaHttpUploader uploader = new MediaHttpUploader(mediaContent, fakeTransport, null);
  uploader.getInitiationHeaders().set("test-header-name", "test-header-value");
  uploader.setProgressListener(new DirectProgressListener());
  // Enable direct media upload.
  uploader.setDirectUploadEnabled(true);

  HttpResponse response = uploader.upload(new GenericUrl(TEST_DIRECT_REQUEST_URL));
  assertEquals(200, response.getStatusCode());

  // There should be only 1 call made for direct media upload.
  assertEquals(1, fakeTransport.lowLevelExecCalls);
}
 
Example 3
Source File: MediaHttpUploaderTest.java    From google-api-java-client with Apache License 2.0 6 votes vote down vote up
public void testDirectMediaUpload() throws Exception {
  int contentLength = MediaHttpUploader.DEFAULT_CHUNK_SIZE * 2;
  MediaTransport fakeTransport = new MediaTransport(contentLength);
  fakeTransport.directUploadEnabled = true;
  InputStream is = new ByteArrayInputStream(new byte[contentLength]);
  InputStreamContent mediaContent = new InputStreamContent(TEST_CONTENT_TYPE, is);
  mediaContent.setLength(contentLength);
  MediaHttpUploader uploader = new MediaHttpUploader(mediaContent, fakeTransport, null);
  uploader.setProgressListener(new DirectProgressListener());
  // Enable direct media upload.
  uploader.setDirectUploadEnabled(true);

  HttpResponse response = uploader.upload(new GenericUrl(TEST_DIRECT_REQUEST_URL));
  assertEquals(200, response.getStatusCode());

  // There should be only 1 call made for direct media upload.
  assertEquals(1, fakeTransport.lowLevelExecCalls);
}
 
Example 4
Source File: MediaHttpUploaderTest.java    From google-api-java-client with Apache License 2.0 6 votes vote down vote up
public void testUploadIOException_WithoutIOExceptionHandler() throws Exception {
  int contentLength = MediaHttpUploader.DEFAULT_CHUNK_SIZE * 2;
  MediaTransport fakeTransport = new MediaTransport(contentLength);
  fakeTransport.testIOException = true;
  InputStream is = new ByteArrayInputStream(new byte[contentLength]);
  InputStreamContent mediaContent = new InputStreamContent(TEST_CONTENT_TYPE, is);
  mediaContent.setLength(contentLength);
  MediaHttpUploader uploader = new MediaHttpUploader(mediaContent, fakeTransport, null);

  try {
    uploader.upload(new GenericUrl(TEST_RESUMABLE_REQUEST_URL));
    fail("expected " + IOException.class);
  } catch (IOException e) {
    // There should be 3 calls made. 1 initiation request, 1 successful upload request,
    // and 1 upload request with server error
    assertEquals(3, fakeTransport.lowLevelExecCalls);
  }
}
 
Example 5
Source File: MediaHttpUploaderTest.java    From google-api-java-client with Apache License 2.0 6 votes vote down vote up
public void testUploadServerError_WithoutUnsuccessfulHandler() throws Exception {
  int contentLength = MediaHttpUploader.DEFAULT_CHUNK_SIZE * 2;
  MediaTransport fakeTransport = new MediaTransport(contentLength);
  fakeTransport.testServerError = true;
  InputStream is = new ByteArrayInputStream(new byte[contentLength]);
  InputStreamContent mediaContent = new InputStreamContent(TEST_CONTENT_TYPE, is);
  mediaContent.setLength(contentLength);
  MediaHttpUploader uploader = new MediaHttpUploader(mediaContent, fakeTransport, null);

  HttpResponse response = uploader.upload(new GenericUrl(TEST_RESUMABLE_REQUEST_URL));
  assertEquals(500, response.getStatusCode());

  // There should be 3 calls made. 1 initiation request, 1 successful upload request and 1 upload
  // request with server error
  assertEquals(3, fakeTransport.lowLevelExecCalls);
}
 
Example 6
Source File: MediaHttpUploaderTest.java    From google-api-java-client with Apache License 2.0 6 votes vote down vote up
public void testDirectMediaUploadWithMetadata() throws Exception {
  int contentLength = MediaHttpUploader.DEFAULT_CHUNK_SIZE * 2;
  MediaTransport fakeTransport = new MediaTransport(contentLength);
  fakeTransport.directUploadEnabled = true;
  fakeTransport.directUploadWithMetadata = true;
  InputStream is = new ByteArrayInputStream(new byte[contentLength]);
  InputStreamContent mediaContent = new InputStreamContent(TEST_CONTENT_TYPE, is);
  mediaContent.setLength(contentLength);
  MediaHttpUploader uploader = new MediaHttpUploader(mediaContent, fakeTransport, null);
  uploader.setProgressListener(new DirectProgressListener());
  // Enable direct media upload.
  uploader.setDirectUploadEnabled(true);
  // Set Metadata
  uploader.setMetadata(new MockHttpContent());

  HttpResponse response = uploader.upload(new GenericUrl(TEST_MULTIPART_REQUEST_URL));
  assertEquals(200, response.getStatusCode());

  // There should be only 1 call made for direct media upload.
  assertEquals(1, fakeTransport.lowLevelExecCalls);
}
 
Example 7
Source File: AbstractGoogleAsyncWriteChannel.java    From hadoop-connectors with Apache License 2.0 6 votes vote down vote up
@Override
public void startUpload(InputStream pipeSource) throws IOException {
  // Connect pipe-source to the stream used by uploader.
  InputStreamContent objectContentStream = new InputStreamContent(contentType, pipeSource);
  // Indicate that we do not know length of file in advance.
  objectContentStream.setLength(-1);
  objectContentStream.setCloseInputStream(false);

  T request = createRequest(objectContentStream);
  request.setDisableGZipContent(true);

  // Change chunk size from default value (10MB) to one that yields higher performance.
  clientRequestHelper.setChunkSize(request, options.getUploadChunkSize());

  // Given that the two ends of the pipe must operate asynchronous relative
  // to each other, we need to start the upload operation on a separate thread.
  uploadOperation = threadPool.submit(new UploadOperation(request, pipeSource));
}
 
Example 8
Source File: GoogleStorageCacheManager.java    From simpleci with MIT License 6 votes vote down vote up
@Override
public void uploadCache(JobOutputProcessor outputProcessor, String cachePath) {
    try {
            outputProcessor.output("Uploading cache file " + cacheFileName + " to google storage\n");

            Storage client = createClient();
            File uploadFile = new File(cachePath);
            InputStreamContent contentStream = new InputStreamContent(
                    null, new FileInputStream(uploadFile));
            contentStream.setLength(uploadFile.length());
            StorageObject objectMetadata = new StorageObject()
                    .setName(cacheFileName);

            Storage.Objects.Insert insertRequest = client.objects().insert(
                    settings.bucketName, objectMetadata, contentStream);

            insertRequest.execute();

            outputProcessor.output("Cache uploaded\n");
    } catch (GeneralSecurityException | IOException e) {
        outputProcessor.output("Error upload cache: " + e.getMessage() + "\n");
    }
}
 
Example 9
Source File: MediaHttpUploaderTest.java    From google-api-java-client with Apache License 2.0 6 votes vote down vote up
public void testResumable_BadResponse() throws IOException {
  int contentLength = 3;
  ResumableErrorMediaTransport fakeTransport = new ResumableErrorMediaTransport();
  byte[] testedData = new byte[contentLength];
  new Random().nextBytes(testedData);
  TestingInputStream is = new TestingInputStream(testedData);
  InputStreamContent mediaContent = new InputStreamContent(TEST_CONTENT_TYPE, is);
  mediaContent.setLength(contentLength);
  MediaHttpUploader uploader =
      new MediaHttpUploader(mediaContent, fakeTransport, new ZeroBackOffRequestInitializer());

  // disable GZip - so we would be able to test byte received by server.
  uploader.setDisableGZipContent(true);
  HttpResponse response = uploader.upload(new GenericUrl(TEST_RESUMABLE_REQUEST_URL));
  assertEquals(500, response.getStatusCode());

  assertTrue("input stream should be closed", is.isClosed);
}
 
Example 10
Source File: AbstractGoogleClientTest.java    From google-api-java-client with Apache License 2.0 5 votes vote down vote up
public void testMediaUpload() throws Exception {
  MediaTransport transport = new MediaTransport();
  AbstractGoogleClient client = new MockGoogleClient.Builder(
      transport, TEST_RESUMABLE_REQUEST_URL, "", JSON_OBJECT_PARSER, null).setApplicationName(
      "Test Application").build();
  InputStream is = new ByteArrayInputStream(new byte[MediaHttpUploader.DEFAULT_CHUNK_SIZE]);
  InputStreamContent mediaContent = new InputStreamContent(TEST_CONTENT_TYPE, is);
  mediaContent.setLength(MediaHttpUploader.DEFAULT_CHUNK_SIZE);
  MockGoogleClientRequest<A> rq =
      new MockGoogleClientRequest<A>(client, "POST", "", null, A.class);
  rq.initializeMediaUpload(mediaContent);
  A result = rq.execute();
  assertEquals("somevalue", result.foo);
}
 
Example 11
Source File: MediaHttpUploaderTest.java    From google-api-java-client with Apache License 2.0 5 votes vote down vote up
public void testUploadOneCall_WithPatch() throws Exception {
  int contentLength = MediaHttpUploader.DEFAULT_CHUNK_SIZE;
  MediaTransport fakeTransport = new MediaTransport(contentLength);
  fakeTransport.testMethodOverride = true;
  InputStream is = new ByteArrayInputStream(new byte[contentLength]);
  InputStreamContent mediaContent = new InputStreamContent(TEST_CONTENT_TYPE, is);
  mediaContent.setLength(contentLength);
  MediaHttpUploader uploader = new MediaHttpUploader(mediaContent, fakeTransport, null);
  uploader.setInitiationRequestMethod(HttpMethods.PATCH);
  uploader.upload(new GenericUrl(TEST_RESUMABLE_REQUEST_URL));

  // There should be 2 calls made. 1 initiation request and 1 upload request.
  assertEquals(2, fakeTransport.lowLevelExecCalls);
}
 
Example 12
Source File: MediaHttpUploaderTest.java    From google-api-java-client with Apache License 2.0 5 votes vote down vote up
public void testUploadOneCall_WithContentSizeProvided() throws Exception {
  int contentLength = MediaHttpUploader.DEFAULT_CHUNK_SIZE;
  MediaTransport fakeTransport = new MediaTransport(contentLength);
  InputStream is = new ByteArrayInputStream(new byte[contentLength]);
  InputStreamContent mediaContent = new InputStreamContent(TEST_CONTENT_TYPE, is);
  mediaContent.setLength(contentLength);
  MediaHttpUploader uploader =
      new MediaHttpUploader(mediaContent, fakeTransport, new GZipCheckerInitializer(true));
  uploader.upload(new GenericUrl(TEST_RESUMABLE_REQUEST_URL));

  // There should be 2 calls made. 1 initiation request and 1 upload request.
  assertEquals(2, fakeTransport.lowLevelExecCalls);
}
 
Example 13
Source File: MediaHttpUploaderTest.java    From google-api-java-client with Apache License 2.0 5 votes vote down vote up
public void testUploadMultipleCalls() throws Exception {
  int contentLength = MediaHttpUploader.DEFAULT_CHUNK_SIZE * 5;
  MediaTransport fakeTransport = new MediaTransport(contentLength);
  InputStream is = new ByteArrayInputStream(new byte[contentLength]);
  InputStreamContent mediaContent = new InputStreamContent(TEST_CONTENT_TYPE, is);
  mediaContent.setLength(contentLength);
  MediaHttpUploader uploader = new MediaHttpUploader(mediaContent, fakeTransport, null);
  uploader.upload(new GenericUrl(TEST_RESUMABLE_REQUEST_URL));

  // There should be 6 calls made. 1 initiation request and 5 upload requests.
  assertEquals(6, fakeTransport.lowLevelExecCalls);
}
 
Example 14
Source File: MediaHttpUploaderTest.java    From google-api-java-client with Apache License 2.0 5 votes vote down vote up
public void testUploadMultipleCalls_WithPatch() throws Exception {
  int contentLength = MediaHttpUploader.DEFAULT_CHUNK_SIZE * 5;
  MediaTransport fakeTransport = new MediaTransport(contentLength);
  fakeTransport.testMethodOverride = true;
  InputStream is = new ByteArrayInputStream(new byte[contentLength]);
  InputStreamContent mediaContent = new InputStreamContent(TEST_CONTENT_TYPE, is);
  mediaContent.setLength(contentLength);
  MediaHttpUploader uploader = new MediaHttpUploader(mediaContent, fakeTransport, null);
  uploader.setInitiationRequestMethod(HttpMethods.PATCH);
  uploader.upload(new GenericUrl(TEST_RESUMABLE_REQUEST_URL));

  // There should be 6 calls made. 1 initiation request and 5 upload requests.
  assertEquals(6, fakeTransport.lowLevelExecCalls);
}
 
Example 15
Source File: MediaHttpUploaderTest.java    From google-api-java-client with Apache License 2.0 5 votes vote down vote up
public void testUploadMultipleCalls_WithSpecifiedHeader() throws Exception {
  int contentLength = MediaHttpUploader.DEFAULT_CHUNK_SIZE * 5;
  MediaTransport fakeTransport = new MediaTransport(contentLength);
  fakeTransport.assertTestHeaders = true;
  InputStream is = new ByteArrayInputStream(new byte[contentLength]);
  InputStreamContent mediaContent = new InputStreamContent(TEST_CONTENT_TYPE, is);
  mediaContent.setLength(contentLength);
  MediaHttpUploader uploader = new MediaHttpUploader(mediaContent, fakeTransport, null);
  uploader.getInitiationHeaders().set("test-header-name", "test-header-value");
  uploader.upload(new GenericUrl(TEST_RESUMABLE_REQUEST_URL));

  // There should be 6 calls made. 1 initiation request and 5 upload requests.
  assertEquals(6, fakeTransport.lowLevelExecCalls);
}
 
Example 16
Source File: MediaHttpUploaderTest.java    From google-api-java-client with Apache License 2.0 5 votes vote down vote up
public void testUploadProgressListener() throws Exception {
  int contentLength = MediaHttpUploader.DEFAULT_CHUNK_SIZE * 2;
  MediaTransport fakeTransport = new MediaTransport(contentLength);
  InputStream is = new ByteArrayInputStream(new byte[contentLength]);
  InputStreamContent mediaContent = new InputStreamContent(TEST_CONTENT_TYPE, is);
  mediaContent.setLength(contentLength);
  MediaHttpUploader uploader = new MediaHttpUploader(mediaContent, fakeTransport, null);
  uploader.setProgressListener(new ResumableProgressListenerWithTwoUploadCalls());
  uploader.upload(new GenericUrl(TEST_RESUMABLE_REQUEST_URL));
}
 
Example 17
Source File: MediaHttpUploaderTest.java    From google-api-java-client with Apache License 2.0 5 votes vote down vote up
public void subtestUpload_ResumableWithError(ErrorType error, int contentLength,
    boolean contentLengthKnown, int maxByteIndexUploadedOnError, int chunks,
    boolean force308OnRangeQueryResponse) throws Exception {
  MediaTransport fakeTransport = new MediaTransport(contentLength, true);
  if (error == ErrorType.IO_EXCEPTION) {
    fakeTransport.testIOException = true;
  } else if (error == ErrorType.SERVER_UNAVAILABLE) {
    fakeTransport.testServerError = true;
  }
  fakeTransport.contentLengthNotSpecified = !contentLengthKnown;
  fakeTransport.maxByteIndexUploadedOnError = maxByteIndexUploadedOnError;
  fakeTransport.force308OnRangeQueryResponse = force308OnRangeQueryResponse;
  byte[] testedData = new byte[contentLength];
  new Random().nextBytes(testedData);
  TestingInputStream is = new TestingInputStream(testedData);
  InputStreamContent mediaContent = new InputStreamContent(TEST_CONTENT_TYPE, is);
  if (contentLengthKnown) {
    mediaContent.setLength(contentLength);
  }
  MediaHttpUploader uploader =
      new MediaHttpUploader(mediaContent, fakeTransport, new ZeroBackOffRequestInitializer());

  // disable GZip - so we would be able to test byte received by server.
  uploader.setDisableGZipContent(true);
  HttpResponse response = uploader.upload(new GenericUrl(TEST_RESUMABLE_REQUEST_URL));
  assertEquals(200, response.getStatusCode());

  // There should be the following number of calls made:
  // 1 initiation request + 1 call to query the range + chunks
  int calls = 2 + chunks;
  assertEquals(calls, fakeTransport.lowLevelExecCalls);

  assertTrue(Arrays.equals(testedData, fakeTransport.bytesReceived));
  assertTrue(is.isClosed);
}
 
Example 18
Source File: MediaHttpUploaderTest.java    From google-api-java-client with Apache License 2.0 5 votes vote down vote up
public void testUploadAuthenticationError() throws Exception {
  int contentLength = MediaHttpUploader.DEFAULT_CHUNK_SIZE * 2;
  MediaTransport fakeTransport = new MediaTransport(contentLength);
  fakeTransport.testAuthenticationError = true;
  InputStream is = new ByteArrayInputStream(new byte[contentLength]);
  InputStreamContent mediaContent = new InputStreamContent(TEST_CONTENT_TYPE, is);
  mediaContent.setLength(contentLength);
  MediaHttpUploader uploader = new MediaHttpUploader(mediaContent, fakeTransport, null);

  HttpResponse response = uploader.upload(new GenericUrl(TEST_RESUMABLE_REQUEST_URL));
  assertEquals(404, response.getStatusCode());

  // There should be only 1 initiation request made with a 404.
  assertEquals(1, fakeTransport.lowLevelExecCalls);
}
 
Example 19
Source File: MediaHttpUploaderTest.java    From google-api-java-client with Apache License 2.0 5 votes vote down vote up
public void testUploadClientErrorInUploadCalls() throws Exception {
  int contentLength = MediaHttpUploader.DEFAULT_CHUNK_SIZE * 2;
  MediaTransport fakeTransport = new MediaTransport(contentLength);
  fakeTransport.testClientError = true;
  InputStream is = new ByteArrayInputStream(new byte[contentLength]);
  InputStreamContent mediaContent = new InputStreamContent(TEST_CONTENT_TYPE, is);
  mediaContent.setLength(contentLength);
  MediaHttpUploader uploader = new MediaHttpUploader(mediaContent, fakeTransport, null);

  HttpResponse response = uploader.upload(new GenericUrl(TEST_RESUMABLE_REQUEST_URL));
  assertEquals(411, response.getStatusCode());

  // There should be 2 calls made. 1 initiation request and 1 upload request that returned a 411.
  assertEquals(2, fakeTransport.lowLevelExecCalls);
}
 
Example 20
Source File: RemoteGoogleDriveConnector.java    From cloudsync with GNU General Public License v2.0 4 votes vote down vote up
@Override
public void update(final Handler handler, final Item item, final boolean with_filedata) throws CloudsyncException, FileIOException
{
	initService(handler);

	int retryCount = 0;
	do
	{
		try
		{
			refreshCredential();

			if (item.isType(ItemType.FILE))
			{
				final File _parentDriveItem = _getHistoryFolder(item);
				if (_parentDriveItem != null)
				{
					final File copyOfdriveItem = new File();
					final ParentReference _parentReference = new ParentReference();
					_parentReference.setId(_parentDriveItem.getId());
					copyOfdriveItem.setParents(Collections.singletonList(_parentReference));
					// copyOfdriveItem.setTitle(driveItem.getTitle());
					// copyOfdriveItem.setMimeType(driveItem.getMimeType());
					// copyOfdriveItem.setProperties(driveItem.getProperties());
					final File _copyOfDriveItem = service.files().copy(item.getRemoteIdentifier(), copyOfdriveItem).execute();
					if (_copyOfDriveItem == null)
					{
						throw new CloudsyncException("Couldn't make a history snapshot of item '" + item.getPath() + "'");
					}
				}
			}
			File driveItem = new File();
			final LocalStreamData data = _prepareDriveItem(driveItem, item, handler, with_filedata);
			if (data == null)
			{
				driveItem = service.files().update(item.getRemoteIdentifier(), driveItem).execute();
			}
			else
			{
				final InputStreamContent params = new InputStreamContent(FILE, data.getStream());
				params.setLength(data.getLength());
				Update updater = service.files().update(item.getRemoteIdentifier(), driveItem, params);
				MediaHttpUploader uploader = updater.getMediaHttpUploader();
				prepareUploader(uploader, data.getLength());
				driveItem = updater.execute();
			}
			if (driveItem == null)
			{
				throw new CloudsyncException("Couldn't update item '" + item.getPath() + "'");
			}
			else if (driveItem.getLabels().getTrashed())
			{
				throw new CloudsyncException("Remote item '" + item.getPath() + "' [" + driveItem.getId() + "] is trashed\ntry to run with --nocache");
			}
			_addToCache(driveItem, null);
			return;
		}
		catch (final IOException e)
		{
			retryCount = validateException("remote update", item, e, retryCount);
			if(retryCount < 0) // TODO workaround - fix this later
				retryCount = 0;
		}
	}
	while (true);
}