Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"head" command for aws s3 to view file contents

On Linux, we generally use the head/tail commands to preview the contents of a file. It helps in viewing a part of the file (to inspect the format for instance), rather than open up the whole file.

In the case of Amazon S3, it seems that there are only ls, cp, mv etc. commands I wanted to know if it is possible to view part of the file without downloading the entire file on my local machine using cp/GET.

like image 582
nutsiepully Avatar asked Sep 22 '14 22:09

nutsiepully


People also ask

How do I view files in 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 view the contents of an AWS file?

Simply running s3head s3://bucket/file will give you the first 10 line of your file. This even supports other head command parameters.

How do I get files from AWS command line S3?

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 .


2 Answers

One thing you could do is cp the object to stout and then pipe it to head:

aws s3 cp s3://path/to/my/object - | head

You get a broken pipe error at the end but it works.

like image 66
dspringate Avatar answered Oct 21 '22 06:10

dspringate


You can specify a byte range when retrieving data from S3 to get the first N bytes, the last N bytes or anything in between. (This is also helpful since it allows you to download files in parallel – just start multiple threads or processes, each of which retrieves part of the total file.)

I don't know which of the various CLI tools support this directly but a range retrieval does what you want.

The AWS CLI tools ("aws s3 cp" to be precise) does not allow you to do range retrieval but s3curl (http://aws.amazon.com/code/128) should do the trick.(So does plain curl, e.g., using the --range parameter but then you would have to do the request signing on your own.)

like image 12
Michael Hanisch Avatar answered Oct 21 '22 05:10

Michael Hanisch