Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Download apk file from S3 using DownloadManager

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=""
like image 289
theDazzler Avatar asked Jul 11 '15 21:07

theDazzler


People also ask

How do I extract files from S3 bucket?

In the Amazon S3 console, choose your S3 bucket, choose the file that you want to open or download, choose Actions, and then choose Open or Download. If you are downloading an object, specify where you want to save it. The procedure for saving the object depends on the browser and operating system that you are using.

How do I download from S3 bucket to local?

You can download an object from an S3 bucket in any of the following ways: Select the object and choose Download or choose Download as from the Actions menu if you want to download the object to a specific folder. If you want to download a specific version of the object, select the Show versions button.

How do I download from S3 bucket to local using AWS CLI?

You can use cp to copy the files from an s3 bucket to your local system. Use the following command: $ aws s3 cp s3://bucket/folder/file.txt . To know more about AWS S3 and its features in detail check this out!


2 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.

like image 129
Yangfan Avatar answered Sep 22 '22 00:09

Yangfan


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
like image 36
luckwithu Avatar answered Sep 24 '22 00:09

luckwithu