CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/maven-com-google-api-client--google-api-client

The Google APIs Client Library for Java is a flexible, efficient, and powerful Java client library for accessing any HTTP-based API on the web, not just Google APIs.

Pending
Overview
Eval results
Files

media-operations.mddocs/

Media Operations

Resumable upload and download functionality for large files with built-in progress tracking, error recovery, and configurable chunk sizes.

Core Imports

import com.google.api.client.googleapis.media.MediaHttpUploader;
import com.google.api.client.googleapis.media.MediaHttpUploaderProgressListener;
import com.google.api.client.googleapis.media.MediaHttpDownloader;
import com.google.api.client.googleapis.media.MediaHttpDownloaderProgressListener;
import com.google.api.client.googleapis.media.MediaUploadErrorHandler;

Media Upload

MediaHttpUploader

Handles resumable media uploads with progress tracking and error recovery.

public final class MediaHttpUploader {
    public static final String CONTENT_LENGTH_HEADER = "X-Upload-Content-Length";
    public static final String CONTENT_TYPE_HEADER = "X-Upload-Content-Type";
    public static final int MINIMUM_CHUNK_SIZE = 262144; // 256KB
    public static final int DEFAULT_CHUNK_SIZE = 10485760; // 10MB
    
    public MediaHttpUploader(AbstractInputStreamContent mediaContent, HttpTransport transport, 
        HttpRequestInitializer httpRequestInitializer);
    
    public HttpResponse upload(GenericUrl initiationRequestUrl) throws IOException;
    
    public MediaHttpUploader setProgressListener(MediaHttpUploaderProgressListener progressListener);
    public MediaHttpUploaderProgressListener getProgressListener();
    
    public MediaHttpUploader setChunkSize(int chunkSize);
    public int getChunkSize();
    
    public MediaHttpUploader setDirectUploadEnabled(boolean directUploadEnabled);
    public boolean isDirectUploadEnabled();
    
    public MediaHttpUploader setDisableGZipContent(boolean disableGZipContent);
    public boolean getDisableGZipContent();
    
    public MediaHttpUploader setInitiationHeaders(HttpHeaders initiationHeaders);
    public HttpHeaders getInitiationHeaders();
    
    public MediaHttpUploader setInitiationRequestMethod(String initiationRequestMethod);
    public String getInitiationRequestMethod();
    
    public MediaHttpUploader setMetadata(HttpContent metadata);
    public HttpContent getMetadata();
    
    public HttpContent getMediaContent();
    public HttpTransport getTransport();
    
    public MediaHttpUploader setSleeper(Sleeper sleeper);
    public Sleeper getSleeper();
    
    public UploadState getUploadState();
    public double getProgress() throws IOException;
    public long getNumBytesUploaded();
    
    public enum UploadState {
        NOT_STARTED,
        INITIATION_STARTED,
        INITIATION_COMPLETE,
        MEDIA_IN_PROGRESS,
        MEDIA_COMPLETE
    }
}

MediaHttpUploaderProgressListener

Interface for tracking upload progress.

public interface MediaHttpUploaderProgressListener {
    void progressChanged(MediaHttpUploader uploader) throws IOException;
}

Usage Example:

import com.google.api.client.googleapis.media.MediaHttpUploader;
import com.google.api.client.googleapis.media.MediaHttpUploaderProgressListener;
import com.google.api.client.http.FileContent;

// Create media content
File mediaFile = new File("path/to/large-file.mp4");
FileContent mediaContent = new FileContent("video/mp4", mediaFile);

// Create uploader
MediaHttpUploader uploader = new MediaHttpUploader(mediaContent, transport, credential);

// Set chunk size (default is 10MB)
uploader.setChunkSize(MediaHttpUploader.DEFAULT_CHUNK_SIZE);

// Add progress listener
uploader.setProgressListener(new MediaHttpUploaderProgressListener() {
    @Override
    public void progressChanged(MediaHttpUploader uploader) throws IOException {
        switch (uploader.getUploadState()) {
            case INITIATION_STARTED:
                System.out.println("Upload initiation started");
                break;
            case INITIATION_COMPLETE:
                System.out.println("Upload initiation completed");
                break;
            case MEDIA_IN_PROGRESS:
                System.out.println("Upload progress: " + 
                    Math.round(uploader.getProgress() * 100) + "%");
                break;
            case MEDIA_COMPLETE:
                System.out.println("Upload completed");
                break;
        }
    }
});

// Perform upload
GenericUrl uploadUrl = new GenericUrl("https://www.googleapis.com/upload/storage/v1/b/bucket/o");
HttpResponse response = uploader.upload(uploadUrl);

Media Download

MediaHttpDownloader

Handles resumable media downloads with progress tracking.

public final class MediaHttpDownloader {
    public static final int MAXIMUM_CHUNK_SIZE = 33554432; // 32MB
    
    public MediaHttpDownloader(HttpTransport transport, HttpRequestInitializer httpRequestInitializer);
    
    public void download(GenericUrl requestUrl, OutputStream outputStream) throws IOException;
    public void download(GenericUrl requestUrl, HttpHeaders requestHeaders, OutputStream outputStream) throws IOException;
    
    public MediaHttpDownloader setProgressListener(MediaHttpDownloaderProgressListener progressListener);
    public MediaHttpDownloaderProgressListener getProgressListener();
    
    public MediaHttpDownloader setChunkSize(int chunkSize);
    public int getChunkSize();
    
    public MediaHttpDownloader setDirectDownloadEnabled(boolean directDownloadEnabled);
    public boolean isDirectDownloadEnabled();
    
    public DownloadState getDownloadState();
    public double getProgress();
    public long getNumBytesDownloaded();
    public long getContentLength();
    
    public MediaHttpDownloader setBytesDownloaded(long bytesDownloaded);
    
    public enum DownloadState {
        NOT_STARTED,
        MEDIA_IN_PROGRESS,
        MEDIA_COMPLETE
    }
}

MediaHttpDownloaderProgressListener

Interface for tracking download progress.

public interface MediaHttpDownloaderProgressListener {
    void progressChanged(MediaHttpDownloader downloader) throws IOException;
}

Usage Example:

import com.google.api.client.googleapis.media.MediaHttpDownloader;
import com.google.api.client.googleapis.media.MediaHttpDownloaderProgressListener;
import java.io.FileOutputStream;

// Create downloader
MediaHttpDownloader downloader = new MediaHttpDownloader(transport, credential);

// Set chunk size for resumable downloads
downloader.setChunkSize(MediaHttpDownloader.MAXIMUM_CHUNK_SIZE);

// Add progress listener
downloader.setProgressListener(new MediaHttpDownloaderProgressListener() {
    @Override
    public void progressChanged(MediaHttpDownloader downloader) throws IOException {
        switch (downloader.getDownloadState()) {
            case MEDIA_IN_PROGRESS:
                System.out.println("Download progress: " + 
                    Math.round(downloader.getProgress() * 100) + "% (" +
                    downloader.getNumBytesDownloaded() + " / " + 
                    downloader.getContentLength() + " bytes)");
                break;
            case MEDIA_COMPLETE:
                System.out.println("Download completed");
                break;
        }
    }
});

// Perform download
GenericUrl downloadUrl = new GenericUrl("https://www.googleapis.com/storage/v1/b/bucket/o/file?alt=media");
FileOutputStream outputStream = new FileOutputStream("downloaded-file.dat");
try {
    downloader.download(downloadUrl, outputStream);
} finally {
    outputStream.close();
}

Error Handling

MediaUploadErrorHandler

Error handler for media upload operations.

public class MediaUploadErrorHandler implements HttpUnsuccessfulResponseHandler {
    public MediaUploadErrorHandler();
    public MediaUploadErrorHandler(BackOff backOff);
    
    public boolean handleResponse(HttpRequest request, HttpResponse response, boolean supportsRetry) throws IOException;
    
    public final BackOff getBackOff();
    public MediaUploadErrorHandler setBackOff(BackOff backOff);
}

Usage Example:

import com.google.api.client.googleapis.media.MediaUploadErrorHandler;
import com.google.api.client.util.ExponentialBackOff;

// Create error handler with exponential backoff
MediaUploadErrorHandler errorHandler = new MediaUploadErrorHandler(
    new ExponentialBackOff.Builder()
        .setInitialIntervalMillis(1000)
        .setMaxElapsedTimeMillis(300000) // 5 minutes
        .build()
);

// Apply to HTTP request
HttpRequest request = transport.createRequestFactory(credential)
    .buildPostRequest(uploadUrl, mediaContent);
request.setUnsuccessfulResponseHandler(errorHandler);

Constants and Configuration

Default Values

// Upload chunk sizes
public static final int MINIMUM_CHUNK_SIZE = 262144; // 256KB for uploads
public static final int DEFAULT_CHUNK_SIZE = 10485760; // 10MB for uploads

// Download chunk sizes  
public static final int MAXIMUM_CHUNK_SIZE = 33554432; // 32MB for downloads

// Upload states
enum UploadState {
    NOT_STARTED,
    INITIATION_STARTED, 
    INITIATION_COMPLETE,
    MEDIA_IN_PROGRESS,
    MEDIA_COMPLETE
}

// Download states
enum DownloadState {
    NOT_STARTED,
    MEDIA_IN_PROGRESS,
    MEDIA_COMPLETE
}

Best Practices

Upload Optimization

// For large files, use appropriate chunk size
uploader.setChunkSize(MediaHttpUploader.DEFAULT_CHUNK_SIZE * 4); // 40MB chunks

// Enable direct upload for small files
if (fileSize < MediaHttpUploader.DEFAULT_CHUNK_SIZE) {
    uploader.setDirectUploadEnabled(true);
}

// Set metadata for the upload
HttpContent metadata = new JsonHttpContent(jsonFactory, fileMetadata);
uploader.setMetadata(metadata);

Download Optimization

// Use maximum chunk size for faster downloads
downloader.setChunkSize(MediaHttpDownloader.MAXIMUM_CHUNK_SIZE);

// Enable direct download for small files
downloader.setDirectDownloadEnabled(true);

// Resume interrupted downloads
if (previouslyDownloadedBytes > 0) {
    downloader.setBytesDownloaded(previouslyDownloadedBytes);
}

Types

AbstractInputStreamContent

Base class for input stream content used in uploads.

HttpContent

Interface for HTTP content.

FileContent

HTTP content implementation for files.

HttpTransport

HTTP transport interface.

HttpRequestInitializer

Interface for initializing HTTP requests.

HttpResponse

HTTP response container.

HttpHeaders

HTTP headers container.

GenericUrl

Generic URL builder.

OutputStream

Java output stream for writing downloaded data.

IOException

Exception for I/O operations.

BackOff

Interface for backoff strategies during retries.

Sleeper

Interface for controlling sleep behavior during uploads.

Install with Tessl CLI

npx tessl i tessl/maven-com-google-api-client--google-api-client

docs

batch-operations.md

client-services.md

http-transport.md

index.md

json-error-handling.md

media-operations.md

oauth2-auth.md

utilities.md

tile.json