I'm writing an Android app and want to use Google Cloud Storage for storing potentially large-ish media files. I'm using the Java client library for the JSON API: com.google.api.services.storage.
My problem is performance. I'm sure I must be doing something wrong. I've got file uploads working but it's almost comically slow. It takes approximately 5 minutes to transfer a 1.5 MB image file, so that's something like 5 kbps, which is going to be unusable for my app. I've enabled billing for my app, but I am on the free tier. Surely this isn't the expected performance level? I'm testing this on a Galaxy S4 on broadband wifi. I am using a Service Account OAUth client key to access GCS.
I've tried with and without gzip encoding, and with and without direct (non-resumable) uploads, and with different chunk sizes - default, minimum, multiples of minimum, etc. I get similar results in all cases. Here's my upload function:
public void uploadFile(String bucketName, String filePath, String mimeType, String gcsFilename, IOProgress ioProgress)
throws IOException {
StorageObject object = new StorageObject();
object.setBucket(bucketName);
File file = new File(filePath);
Long fileSize = file.length();
Log.d(TAG, "uploadFile START: " + bucketName + ":" + gcsFilename + " -> " + filePath);
try (InputStream stream = new FileInputStream(file)) {
InputStreamContent content = new InputStreamContent(mimeType,
stream);
Storage.Objects.Insert insert = storage.objects().insert(
bucketName, null, content);
insert.setName(gcsFilename);
insert.getMediaHttpUploader().setDisableGZipContent(true); // this seems to help to disable... at least when debugging
// insert.getMediaHttpUploader().setDirectUploadEnabled(true);
insert.getMediaHttpUploader().setChunkSize(MediaHttpUploader.MINIMUM_CHUNK_SIZE);
if (ioProgress != null) {
insert.getMediaHttpUploader().setProgressListener(new CloudUploadProgressListener(ioProgress, fileSize));
}
insert.execute();
Log.d(TAG, "uploadFile FINISH: " + bucketName + ":" + gcsFilename + " -> " + filePath);
}
}
My problem was two-fold.
1) Instead of using com.google.api.client.http.javanet.NetHttpTransport, I used com.google.api.client.http.apache.ApacheHttpTransport and saw approximately a 5x speed increase.
This also fixed a separate problem where I got socket errors trying to using Google Cloud Endpoints (with a NetHttpTransport) immediately after doing a GCS transfer.
2) Trying to run the upload while debugging with ADB accounted for the rest of the difference. Running in non-debug mode with ApacheHttpTransport brought my transfer speeds up to about 1.4 Mbps, where my local network has about a 1.8 Mbps capacity.
By the way, I attempted to use JetS3t but ran into this exact problem: IncompatibleClassChangeError exception is thrown when using JetS3t on Android
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With