当前位置: 首页>>代码示例>>Java>>正文


Java MediaHttpUploaderProgressListener类代码示例

本文整理汇总了Java中com.google.api.client.googleapis.media.MediaHttpUploaderProgressListener的典型用法代码示例。如果您正苦于以下问题:Java MediaHttpUploaderProgressListener类的具体用法?Java MediaHttpUploaderProgressListener怎么用?Java MediaHttpUploaderProgressListener使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。


MediaHttpUploaderProgressListener类属于com.google.api.client.googleapis.media包,在下文中一共展示了MediaHttpUploaderProgressListener类的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: upload

import com.google.api.client.googleapis.media.MediaHttpUploaderProgressListener; //导入依赖的package包/类
@Override
public Handle<?> upload(LogFilePath localPath) throws Exception {
    final String gsBucket = mConfig.getGsBucket();
    final String gsKey = localPath.withPrefix(mConfig.getGsPath()).getLogFilePath();
    final File localFile = new File(localPath.getLogFilePath());
    final boolean directUpload = mConfig.getGsDirectUpload();

    LOG.info("uploading file {} to gs://{}/{}", localFile, gsBucket, gsKey);

    final StorageObject storageObject = new StorageObject().setName(gsKey);
    final FileContent storageContent = new FileContent(Files.probeContentType(localFile.toPath()), localFile);

    final Future<?> f = executor.submit(new Runnable() {
        @Override
        public void run() {
            try {
                Storage.Objects.Insert request = mClient.objects().insert(gsBucket, storageObject, storageContent);

                if (directUpload) {
                    request.getMediaHttpUploader().setDirectUploadEnabled(true);
                }

                request.getMediaHttpUploader().setProgressListener(new MediaHttpUploaderProgressListener() {
                    @Override
                    public void progressChanged(MediaHttpUploader uploader) throws IOException {
                        LOG.debug("[{} %] upload file {} to gs://{}/{}",
                                (int) uploader.getProgress() * 100, localFile, gsBucket, gsKey);
                    }
                });

                request.execute();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    });

    return new FutureHandle(f);
}
 
开发者ID:pinterest,项目名称:secor,代码行数:40,代码来源:GsUploadManager.java

示例2: run

import com.google.api.client.googleapis.media.MediaHttpUploaderProgressListener; //导入依赖的package包/类
/**
 * Uploads {@link #setArchive(File) specified file} to Google Drive.
 */
@TaskAction
public void run() {
	try {
		Preconditions.checkNotNull(this.clientId, "Google Drive client ID must not be null");
		Preconditions.checkNotNull(this.clientSecret, "Google Drive client secret must not be null");
		Preconditions.checkNotNull(this.accessToken, "Google Drive access token must not be null");
		Preconditions.checkNotNull(this.refreshToken, "Google Drive refresh token must not be null");
		Preconditions.checkNotNull(this.archive, "Archive must not be null");
		Preconditions.checkArgument(this.archive.exists(), "Archive must exist");
		Preconditions.checkArgument(this.archive.isFile(), "Archive must be a file");

		final Drive drive = constructDrive();

		final com.google.api.services.drive.model.File parent = locateParent(drive);

		final com.google.api.services.drive.model.File descriptor = new com.google.api.services.drive.model.File();
		final FileContent content = new FileContent(mimeType, archive);

		if (null != parent) {
			descriptor.setParents(Arrays.<ParentReference>asList(new ParentReference().setId(parent.getId())));
		}
		descriptor.setMimeType(content.getType());
		descriptor.setTitle(content.getFile().getName());

		final Drive.Files.Insert insert = drive.files().insert(descriptor, content);
		final MediaHttpUploader uploader = insert.getMediaHttpUploader();

		uploader.setChunkSize(1 * 1024 * 1024 /* bytes */);

		if (listenForUpload) {
			uploader.setProgressListener(new MediaHttpUploaderProgressListener() {
				@Override
				public void progressChanged(MediaHttpUploader u) throws IOException {
					final double progress = (double) u.getNumBytesUploaded() / content.getLength();

					System.out.printf("\r[%-50.50s] %.2f%%", Strings.repeat("#", (int) (progress * 50)), progress * 100);
					System.out.flush();
				}
			});
		}

		insert.execute();
	} catch (Exception e) {
		throw new TaskExecutionException(this, e);
	}
}
 
开发者ID:madhead,项目名称:gradle-backup-plugin,代码行数:50,代码来源:GoogleDriveUploadTask.java

示例3: testSimpleCopyInternal

import com.google.api.client.googleapis.media.MediaHttpUploaderProgressListener; //导入依赖的package包/类
private void testSimpleCopyInternal(String key, String[] args, List<S3ObjectSummary> objectSummaries) throws Exception {
    main = new MirrorMain(args);
    main.parseArguments();
    main.setSourceClient(s3);
    main.setDestClient(gcs);
    MirrorOptions options = main.getOptions();
    MirrorContext context = new MirrorContext(options);
    main.setContext(context);
    MirrorMaster master = new MirrorMaster(s3, gcs, context);

    GoogleJsonResponseException e = PowerMockito.mock(GoogleJsonResponseException.class);
    PowerMockito.when(e.getStatusCode()).thenReturn(404);


    main.setMaster(master);

    PowerMockito.when(s3.listObjects(Mockito.any(ListObjectsRequest.class))).thenReturn(listing);
    PowerMockito.when(listing.getObjectSummaries()).thenReturn(objectSummaries);

    Storage.Objects objects = PowerMockito.mock(Storage.Objects.class);
    Storage.Objects.Get getObject = PowerMockito.mock(Storage.Objects.Get.class);
    PowerMockito.when(gcs.objects()).thenReturn(objects);
    PowerMockito.when(gcs.objects().get(DESTINATION, key)).thenReturn(getObject);
    PowerMockito.when(getObject.execute()).thenThrow(e);

    //mock object from S3
    S3Object s3Object = new S3Object();
    s3Object.setKey(key);
    String content = "S3Object_Content";
    InputStream inputStream = new ByteArrayInputStream(content.getBytes());
    s3Object.setObjectContent(inputStream);
    ObjectMetadata objectMetadata = new ObjectMetadata();
    objectMetadata.setContentType("text");
    objectMetadata.setContentLength(10);
    objectMetadata.setHeader(Headers.ETAG, "etag");

    s3Object.setObjectMetadata(objectMetadata);
    s3Object.setBucketName(SOURCE);

    //return ACL
    PowerMockito.when(s3.getObjectAcl(SOURCE, key)).thenReturn(objectAcl);

    PowerMockito.when(s3.getObjectMetadata(SOURCE, key)).thenReturn(objectMetadata);

    PowerMockito.when(s3.getObject(any(GetObjectRequest.class))).thenReturn(s3Object);


    Storage.Objects.Insert insertObject = PowerMockito.mock(Storage.Objects.Insert.class);
    PowerMockito.when(gcs.objects().insert(any(String.class), any(StorageObject.class), any(InputStreamContent.class))).thenReturn(insertObject);

    MediaHttpUploader mediaHttpUploader = PowerMockito.mock(MediaHttpUploader.class);
    PowerMockito.when(insertObject.getMediaHttpUploader()).thenReturn(mediaHttpUploader);
    PowerMockito.when(mediaHttpUploader.setProgressListener(any(MediaHttpUploaderProgressListener.class))).thenReturn(mediaHttpUploader);
    PowerMockito.when(mediaHttpUploader.setDisableGZipContent(any(boolean.class))).thenReturn(mediaHttpUploader);

    main.init();
    main.run();

    //number of copied files
    assertEquals(1, main.getContext().getStats().objectsCopied.get());

    //size of total copied files
    assertEquals(10, main.getContext().getStats().bytesCopied.get());
}
 
开发者ID:DevOps-TangoMe,项目名称:BucketSyncer,代码行数:65,代码来源:GCS_MockTest.java


注:本文中的com.google.api.client.googleapis.media.MediaHttpUploaderProgressListener类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。