private void pushTaskFile(final File logFile, final String taskKey) throws IOException { FileInputStream fileStream = new FileInputStream(logFile); InputStreamContent mediaContent = new InputStreamContent("text/plain", fileStream); mediaContent.setLength(logFile.length()); try { RetryUtils.retry( (RetryUtils.Task<Void>) () -> { storage.insert(config.getBucket(), taskKey, mediaContent); return null; }, GoogleUtils::isRetryable, 1, 5 ); } catch (IOException e) { throw e; } catch (Exception e) { throw new RE(e, "Failed to upload [%s] to [%s]", logFile, taskKey); } }
mediaContent.setLength(bts.length);
/** {@inheritDoc} */ @Override public void registerAddresses(Collection<InetSocketAddress> addrs) throws IgniteSpiException { assert !F.isEmpty(addrs); init(); for (InetSocketAddress addr : addrs) { String key = keyFromAddr(addr); StorageObject object = new StorageObject(); object.setBucket(bucketName); object.setName(key); InputStreamContent content = new InputStreamContent("application/octet-stream", OBJECT_CONTENT); content.setLength(OBJECT_CONTENT.available()); try { Storage.Objects.Insert insertObject = storage.objects().insert(bucketName, object, content); insertObject.execute(); } catch (Exception e) { throw new IgniteSpiException("Failed to put entry [bucketName=" + bucketName + ", entry=" + key + ']', e); } } }
/** * 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(); } // [END upload_stream]
private static final java.io.File UPLOAD_FILE = new java.io.File(UPLOAD_FILE_PATH); /** Uploads a file using either resumable or direct media upload. */ private static File uploadFile(boolean useDirectUpload) throws IOException { File fileMetadata = new File(); fileMetadata.setTitle(UPLOAD_FILE.getName()); InputStreamContent mediaContent = new InputStreamContent("image/jpeg", new BufferedInputStream( new FileInputStream(UPLOAD_FILE))); mediaContent.setLength(UPLOAD_FILE.length()); Drive.Files.Insert insert = drive.files().insert(fileMetadata, mediaContent); MediaHttpUploader uploader = insert.getMediaHttpUploader(); uploader.setDirectUploadEnabled(useDirectUpload); uploader.setProgressListener(new FileUploadProgressListener()); return insert.execute(); }
File mediaFile = new File("/tmp/Test.jpg"); InputStreamContent mediaContent = new InputStreamContent("image/jpeg", new BufferedInputStream(new FileInputStream(mediaFile))); mediaContent.setLength(mediaFile.length()); MediaHttpUploader uploader = new MediaHttpUploader(mediaContent, transport, httpRequestInitializer); uploader.setProgressListener(new CustomProgressListener()); HttpResponse response = uploader.upload(requestUrl); if (!response.isSuccessStatusCode()) { throw GoogleJsonResponseException(jsonFactory, response); }
InputStreamContent mediaContent = new InputStreamContent("image/jpeg", new BufferedInputStream( new FileInputStream(UPLOAD_FILE))); mediaContent.setLength(UPLOAD_FILE.length()); Insert insert = drive.files().insert(fileMetadata, mediaContent); MediaHttpUploader uploader = insert.getMediaHttpUploader(); uploader.setDirectUploadEnabled(false); uploader.setProgressListener(new FileUploadProgressListener()); insert.execute();
mediaContent.setLength(fileContent.length());
@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"); } }
/** * 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. PipedInputStream pipeSource = new PipedInputStream(pipeBufferSize); OutputStream 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); HttpHeaders headers = clientRequestHelper.getRequestHeaders(request); // 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, uploadChunkSize); // 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; }
/** * 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. PipedInputStream pipeSource = new PipedInputStream(pipeBufferSize); OutputStream 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); HttpHeaders headers = clientRequestHelper.getRequestHeaders(request); // 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, uploadChunkSize); // 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; }
mediaContent.setLength(fileSize);
contentChunk = new InputStreamContent( mediaContent.getType(), limitInputStream).setRetrySupported(true) .setLength(blockSize).setCloseInputStream(false); mediaContentLengthStr = String.valueOf(getMediaContentLength()); } else {