本文整理匯總了Java中com.google.api.client.http.InputStreamContent.setLength方法的典型用法代碼示例。如果您正苦於以下問題:Java InputStreamContent.setLength方法的具體用法?Java InputStreamContent.setLength怎麽用?Java InputStreamContent.setLength使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類com.google.api.client.http.InputStreamContent
的用法示例。
在下文中一共展示了InputStreamContent.setLength方法的13個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: recursiveGCSSync
import com.google.api.client.http.InputStreamContent; //導入方法依賴的package包/類
private void recursiveGCSSync(File repo, String prefix, Storage gcs) throws IOException {
for (String name : repo.list()) {
if (!name.equals(".git")) {
File file = new File(repo.getPath(), name);
if (file.isDirectory()) {
recursiveGCSSync(file, prefix + file.getName() + "/", gcs);
} else {
InputStream inputStream = new FileInputStream(repo.getPath() + "/" + file.getName());
InputStreamContent mediaContent = new InputStreamContent("text/plain", inputStream);
mediaContent.setLength(file.length());
StorageObject objectMetadata = new StorageObject()
.setName(prefix + file.getName());
gcs.objects().insert(bucket, objectMetadata, mediaContent).execute();
}
}
}
}
示例2: uploadFile
import com.google.api.client.http.InputStreamContent; //導入方法依賴的package包/類
/**
* 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.
*/
private 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();
LogUtils.debug(LOG_TAG, "Successfully uploaded file to bucket.\nName: " + name + "\nBucket name: " +
bucketName);
}
示例3: uploadCache
import com.google.api.client.http.InputStreamContent; //導入方法依賴的package包/類
@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");
}
}
示例4: putBytes
import com.google.api.client.http.InputStreamContent; //導入方法依賴的package包/類
/** Upload data. */
public static boolean putBytes(final String bucket, final String key, final byte[] bytes) {
final InputStreamContent mediaContent =
new InputStreamContent("application/octet-stream", new ByteArrayInputStream(bytes));
mediaContent.setLength(bytes.length);
try {
final Storage.Objects.Insert insertObject =
client.objects().insert(bucket, null, mediaContent);
insertObject.setName(key);
if (mediaContent.getLength() > 0
&& mediaContent.getLength() <= 2 * 1000 * 1000 /* 2MB */) {
insertObject.getMediaHttpUploader().setDirectUploadEnabled(true);
}
insertObject.execute();
return true;
} catch (IOException e) {
LOGGER.error("Error uploading data", e);
return false;
}
}
示例5: uploadFile
import com.google.api.client.http.InputStreamContent; //導入方法依賴的package包/類
/**
* 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();
}
示例6: uploadFile
import com.google.api.client.http.InputStreamContent; //導入方法依賴的package包/類
public static com.google.api.services.drive.model.File uploadFile(
java.io.File file, Credential credential, String parent)
throws IOException {
com.google.api.services.drive.model.File fileMetadata = new com.google.api.services.drive.model.File();
fileMetadata.setParents(Arrays.asList(new ParentReference()
.setId(parent)));
fileMetadata.setTitle(file.getName());
InputStreamContent mediaContent = new InputStreamContent(
"image/png", new BufferedInputStream(new FileInputStream(
file)));
mediaContent.setLength(file.length());
Drive.Files.Insert insert = drive.files().insert(fileMetadata,
mediaContent);
MediaHttpUploader uploader = insert.getMediaHttpUploader();
uploader.setDirectUploadEnabled(true);
return insert.execute();
}
示例7: uploadFile
import com.google.api.client.http.InputStreamContent; //導入方法依賴的package包/類
public File uploadFile(String name, String mimeType, java.io.File mediaFile, boolean onWifi)
throws IOException, JSONException {
// File Metadata
File fileMetadata = new File();
fileMetadata.setTitle(name);
fileMetadata.setMimeType(mimeType);
// Set the parent folder.
ParentReference uploadDir = new ParentReference();
uploadDir.setId(findUploadDirectory().getId());
fileMetadata.setParents(Arrays.asList(uploadDir));
InputStreamContent mediaContent = new InputStreamContent(mimeType, new BufferedInputStream(
new FileInputStream(mediaFile)));
mediaContent.setLength(mediaFile.length());
Drive.Files.Insert insert = drive.files().insert(fileMetadata, mediaContent);
insert.getMediaHttpUploader().setProgressListener(new ProgressListener(mediaFile));
insert.getMediaHttpUploader().setBackOffPolicyEnabled(true);
int chunkSize = onWifi ? MediaHttpUploader.MINIMUM_CHUNK_SIZE * 2
: MediaHttpUploader.MINIMUM_CHUNK_SIZE;
insert.getMediaHttpUploader().setChunkSize(chunkSize);
return insert.execute();
}
示例8: exportOnline
import com.google.api.client.http.InputStreamContent; //導入方法依賴的package包/類
/**
* Backup database to google docs
*
* @param drive Google docs connection
* @param targetFolder Google docs folder name
* */
public String exportOnline(Drive drive, String targetFolder) throws Exception {
// get folder first
String folderId = GoogleDriveClient.getOrCreateDriveFolder(drive, targetFolder);
if (folderId == null) {
throw new ImportExportException(R.string.gdocs_folder_not_found);
}
// generation backup file
String fileName = generateFilename();
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
OutputStream out = new BufferedOutputStream(new GZIPOutputStream(outputStream));
generateBackup(out);
// transforming streams
InputStream backup = new ByteArrayInputStream(outputStream.toByteArray());
InputStreamContent mediaContent = new InputStreamContent(BACKUP_MIME_TYPE, new BufferedInputStream(backup));
mediaContent.setLength(outputStream.size());
// File's metadata.
com.google.api.services.drive.model.File body = new com.google.api.services.drive.model.File();
body.setTitle(fileName);
body.setMimeType(BACKUP_MIME_TYPE);
body.setFileSize((long)outputStream.size());
List<ParentReference> parentReference = new ArrayList<ParentReference>();
parentReference.add(new ParentReference().setId(folderId)) ;
body.setParents(parentReference);
com.google.api.services.drive.model.File file = drive.files().insert(body, mediaContent).execute();
return fileName;
}
示例9: getStagedURI
import com.google.api.client.http.InputStreamContent; //導入方法依賴的package包/類
private String getStagedURI(Cluster cluster, String uri) {
URI parsed = URI.create(uri);
if (!isNullOrEmpty(parsed.getScheme()) && !"file".equals(parsed.getScheme())) {
return uri;
}
// either a file URI or just a path, stage it to GCS
File local = Paths.get(uri).toFile();
String configBucket = cluster.getConfig().getConfigBucket();
LOG.debug("Staging {} in GCS bucket {}", uri, configBucket);
try {
InputStreamContent content = new InputStreamContent(
"application/octet-stream", new FileInputStream(local));
content.setLength(local.length()); // docs say that setting length improves upload perf
StorageObject object = new StorageObject()
.setName(Paths.get(GCS_STAGING_PREFIX, local.getName()).toString());
object = client.storage().objects()
.insert(configBucket, object, content)
.execute();
return new URI("gs", object.getBucket(), "/" + object.getName(), null).toString();
} catch (URISyntaxException | IOException e) {
throw Throwables.propagate(e);
}
}
示例10: exportOnline
import com.google.api.client.http.InputStreamContent; //導入方法依賴的package包/類
public String exportOnline(Drive drive, String targetFolder) throws Exception {
// get folder first
String folderId = GoogleDriveClient.getOrCreateDriveFolder(drive, targetFolder);
if (folderId == null) {
throw new ImportExportException(R.string.gdocs_folder_not_configured);
}
// generation backup file
String fileName = generateFilename();
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
OutputStream out = new BufferedOutputStream(new GZIPOutputStream(outputStream));
generateBackup(out);
// transforming streams
InputStream backup = new ByteArrayInputStream(outputStream.toByteArray());
InputStreamContent mediaContent = new InputStreamContent(BACKUP_MIME_TYPE, new BufferedInputStream(backup));
mediaContent.setLength(outputStream.size());
// File's metadata.
com.google.api.services.drive.model.File body = new com.google.api.services.drive.model.File();
body.setTitle(fileName);
body.setMimeType(BACKUP_MIME_TYPE);
body.setFileSize((long)outputStream.size());
List<ParentReference> parentReference = new ArrayList<ParentReference>();
parentReference.add(new ParentReference().setId(folderId)) ;
body.setParents(parentReference);
com.google.api.services.drive.model.File file = drive.files().insert(body, mediaContent).execute();
return fileName;
}
示例11: initialize
import com.google.api.client.http.InputStreamContent; //導入方法依賴的package包/類
/**
* Initialize this channel object for writing.
*
* @throws IOException
*/
public void initialize() throws IOException {
// Create a pipe such that its one end is connected to the input stream used by
// the uploader and the other end is the write channel used by the caller.
pipeSource = new PipedInputStream(pipeBufferSize);
pipeSink = new PipedOutputStream(pipeSource);
pipeSinkChannel = Channels.newChannel(pipeSink);
// 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);
// Set a larger backend upload-chunk granularity than system defaults.
HttpHeaders headers = clientRequestHelper.getRequestHeaders(request);
headers.set("X-Goog-Upload-Desired-Chunk-Granularity",
Math.min(GCS_UPLOAD_GRANULARITY, uploadBufferSize));
// Legacy check. Will be phased out.
if (limitFileSizeTo250Gb) {
headers.set("X-Goog-Upload-Max-Raw-Size", UPLOAD_MAX_SIZE);
}
// Change chunk size from default value (10MB) to one that yields higher performance.
clientRequestHelper.setChunkSize(request, uploadBufferSize);
// 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));
isInitialized = true;
}
示例12: upload
import com.google.api.client.http.InputStreamContent; //導入方法依賴的package包/類
@Override
public void upload(final Handler handler, final Item item) throws CloudsyncException, FileIOException
{
initService(handler);
String title = handler.getLocalProcessedTitle(item);
File parentDriveItem = null;
File driveItem;
int retryCount = 0;
do
{
try
{
refreshCredential();
parentDriveItem = _getDriveItem(item.getParent());
final ParentReference parentReference = new ParentReference();
parentReference.setId(parentDriveItem.getId());
driveItem = new File();
driveItem.setTitle(title);
driveItem.setParents(Collections.singletonList(parentReference));
final LocalStreamData data = _prepareDriveItem(driveItem, item, handler, true);
if (data == null)
{
driveItem = service.files().insert(driveItem).execute();
}
else
{
final InputStreamContent params = new InputStreamContent(FILE, data.getStream());
params.setLength(data.getLength());
Insert inserter = service.files().insert(driveItem, params);
MediaHttpUploader uploader = inserter.getMediaHttpUploader();
prepareUploader(uploader, data.getLength());
driveItem = inserter.execute();
}
if (driveItem == null)
{
throw new CloudsyncException("Couldn't create item '" + item.getPath() + "'");
}
_addToCache(driveItem, null);
item.setRemoteIdentifier(driveItem.getId());
return;
}
catch (final IOException e)
{
if (parentDriveItem != null)
{
for (int i = 0; i < MIN_SEARCH_RETRIES; i++)
{
driveItem = _searchDriveItem(item.getParent(), title);
if (driveItem != null)
{
LOGGER.log(Level.WARNING, "Google Drive IOException: " + getExceptionMessage(e) + " - found partially uploaded item - try to update");
item.setRemoteIdentifier(driveItem.getId());
update(handler, item, true);
return;
}
LOGGER.log(Level.WARNING, "Google Drive IOException: " + getExceptionMessage(e) + " - item not uploaded - retry " + (i + 1) + "/" + MIN_SEARCH_RETRIES + " - wait "
+ MIN_SEARCH_BREAK + " ms");
sleep(MIN_SEARCH_BREAK);
}
}
retryCount = validateException("remote upload", item, e, retryCount);
if( retryCount == -1 ) // ignore a failing item (workaround for now)
return;
}
}
while (true);
}
示例13: update
import com.google.api.client.http.InputStreamContent; //導入方法依賴的package包/類
@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);
}