Home » Android » android – Download apk file from S3 using DownloadManager

android – Download apk file from S3 using DownloadManager

Posted by: admin May 14, 2020 Leave a comment

Questions:

Is there a way to download a file from an S3 bucket using Android’s DownloadManager?

I can currently download an apk file from Dropbox doing this:

DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));

request.addRequestHeader("Content-Type", "application/vnd.android.package-archive");
request.setMimeType("application/vnd.android.package-archive");

final DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
final long id = manager.enqueue(request);

Now what I want to do is replace “url” with my Amazon URL. When I do this, the download just stays at 0% and just gets stuck there.

Is there any way to do this?

By the way, the Amazon URL I pass in contains the AWSAccessKeyId, Expires, and Signature parameters. The url looks like this:

https://bucket-name.s3-us-west-2.amazonaws.com/uploads/app/apk/22/app.apk?AWSAccessKeyId=""&Expires=""&Signature=""
How to&Answers:

The reason it fails is because you set the Content-Type header. This header isn’t needed for HTTP download. However, once set, it has to part of the signature in the presigned URL, as required by AWS S3. Or else you will see 403 Forbidden – SignatureDoesNotMatch error.

The quick solution is to remove the Content-Type header.

request.addRequestHeader("Content-Type", "application/vnd.android.package-archive");

Another solution is to generate a presigned URL with headers you need. Say you really want to set content type, you can add it to GeneratePresignedUrlRequest and then create an URL from it.

GeneratePresignedUrlRequest request = new GeneratePresignedUrlRequest(bucket, key);
request.setContentType("application/vnd.android.package-archive");
URL url = s3Client.generatePresignedUrl(request);

See AmazonS3.generatePresignedUrl(GeneratePresignedUrlRequest) for more details.

Answer:

Hello You can use the following header to get the apk file in request.

Accept-Encoding:gzip, deflate
Accept-Language:en-US,en;q=0.8
Cache-Control:no-cache
Connection:keep-alive
Content-Length:819457
Content-Type:application/octet-stream