Amazon S3 see private files - amazon-web-services

I am using Amazon S3 to upload files into different folders. All folders and files are public and can be seen by anyone. I created a private folder, where i want to put private images so that only i can see them. I already created a bucket policy rule that will deny the access to that folder. But how can i see the files ? Is there a special link like this https://s3.amazonaws.com/bucket/private_folder/file.jpg?secret_key=123 that will let me and someone who know`s that secret key to see the files ?
Is there any way of uploading private files that can be seen by using a secret_key, url or something like that ?

By default, all objects in Amazon S3 are private. Objects can then be made "public" by adding permissions, via one of:
Object Access Control List (ACL): Setting the permission directly on the object
Bucket Policy: Relates to the bucket, can define rules relating to sub-directories, key name (filenames), time-of-day, IP address, etc
IAM Policy: Relates to specific Users or Groups
As long as one of these methods grants access, the person will be able to access the object. It is also possible to assign Deny permissions that override Allow permissions.
When an object is being accessed via an un-authenticated URL (eg s3.amazonaws.com/bucket-name/object-key), the above rules determine access. However, even "private" files can be accessed if you authenticate against the service, such as calling an S3 API with your user credentials or using a pre-signed URL.
To see how this works, click a private file in the Amazon S3 Management Console, then choose Open from the Actions menu. The object will be opened. This is done by providing the browser with a pre-signed URL that includes a cryptographically-sized URL and a period of validity. The URL will work to Get the private file only until a defined time.
So, to answer your question, you can still access private files via:
The Open command in the console
Pre-Signed URLs in a web browser
Authenticated API calls
Just be careful that you don't define DENY rules that override even your ability to access files. It's easier to simply ALLOW the directories you'd like to be public.
See: Query String Request Authentication Alternative

You may access s3 private file by creating a temporary Url using temporaryUrl method.
$url = Storage::temporaryUrl(
'file.jpg', now()->addMinutes(5)
);
The params of temporaryUrl method are $path, $expiration and $option = []. The first two params are required and the default value of $option is an empty array.

Related

AWS s3 - difference between "Download" and "Object URL"

Read as I might, I can't find the answer. I have set up a bucket and user with GetObject permission. In the AWS console, I can use the download and open links (green arrows below) successfully, which seems to indicate the permissions are set right. However, when clicking on the Object URL link (red arrow below), I get an Access Denied XML error.
What is the purpose of the Object URL? What is the difference between it and the download/open buttons? Also, why is the owner field blank? I left the config to default which "should" have the uploader as the owner, no?
By default all buckets and objects are private and not accessible from the internet. To make your private objects accessible from the internet without the need for IAM credentials, you have to create S3 pre-signed url. And this is exactly what open/download links do - they generate S3 pre-signed url for you to use. So when you click them, AWS will generate the S3 pre-signed urls and a browser will request the object using the url.
Clicking Object URL does not work, because when browser makes request to AWS for that object, it does not sign the request using IAM credentials. The Object URL would only work if the bucket or the object allowed for anonymous access. In that case, no IAM credentials are required. This is mostly useful for serving static webpages from S3.
Simply put, Object URL is an external link which checks for public permissions for access.
Download and Open uses your currently signed-in user permissions to verify whether you should have access to them, which is why they work for you.
The owner field may be blank because it was uploaded by a public/anonymous user that doesn't have an IAM User in your system.
By default, an Amazon S3 object is owned by the identity that uploaded the object. This means that if you allow public write access to your bucket, the objects uploaded by public (anonymous) users are publicly owned.
https://aws.amazon.com/premiumsupport/knowledge-center/s3-object-change-anonymous-ownership/

how to assign Amazon S3 objects permissions to a particular IAM group?

How to allow read/write/delete etc, permissions to users in a particular IAM group for a specific Amazon S3 object/file.
If you wish to control access to "millions" of individual files where access is not based upon the path (directory/folder) of the files, then you will need to create your own authentication method.
This can be done by using an Amazon S3 Pre-signed URL. Basically:
Users access your application
When they request access to a secure file (or, for example, when the application generates an HTML page that includes a link to such a file, or even a reference in an Image tag), the application generates a time-limited pre-signed URL
Users can use this link/URL to access the object in Amazon S3
After the expiry period, the URL no longer works
This gives your application full control over whether a user can access an object.
The only alternative if you were to use IAM would be to grant access based upon the path of the object. It is not a good method to assign access to individual objects.

S3 website endpoint access

The s3 website endpoint docs say:
"In order for your customers to access content at the website
endpoint, you must make all your content publicly readable."
Does this mean:
Configuring a bucket for website hosting automatically makes all of
that bucket's content readable via the website endpoint (regardless
of per object permissioning)
OR
Per object permissioning can prevent specific content from being accessible via the website endpoint while other content is accessible via the website endpoint.
or some other explanation I'm not thinking of.
Simply enabling the web site hosting feature does not implicitly make the entire bucket public.
The web site endpoint for the bucket does not support accessing private objects using pre-signed URLs or the Authorization header... so if you want to make objects accessible, you have to do it explicitly at either the object or bucket level, using ACLs or policy statements.
If you don't make them accessible, they remain inaccessible.
Two aspects here because there are two different ways you can give permissions to bucket,
Bucket level permission (Permission for all files in the bucket)
Object permission (permission for each file inside the bucket)
If you want bucket level permission then you have to create a bucket policy and in that bucket policy if you gave permission to all (Principal: "*") then everybody can access all files in the bucket. Ideally, if you are hosting a static website in an S3 bucket then you should give full read-only permission to the public for all files. In this scenario, you can set a bucket level policy to give read-only permission to the public.
Object level permission you can set by access control list and you can give it for each object in the bucket. For example, if you have 10 files in the bucket and you have given only two files public access then only that file can be accessed by public not the remaining ones

Amazon S3 download authentication

I have created a bucket in Amazon S3 and have uploaded 2 files in it and made them public. I have the links through which I can access them from anywhere on the Internet. I now want to put some restriction on who can download the files. Can someone please help me with that. I did try the documentation, but got confused.
I want that at the time of download using the public link it should ask for some credentials or something to authenticate the user at that time. Is this possible?
By default, all objects in Amazon S3 are private. You can then add permissions so that people can access your objects. This can be done via:
Access Control List permissions on individual objects
A Bucket Policy
IAM Users and Groups
A Pre-Signed URL
As long as at least one of these methods is granting access, your users will be able to access the objects from Amazon S3.
1. Access Control List on individual objects
The Make Public option in the Amazon S3 management console will grant Open/Download permissions to all Internet users. This can be used to grant public access to specific objects.
2. Bucket Policy
A Bucket Policy can be used to grant access to a whole bucket or a portion of a bucket. It can also be used to specify limits to access. For example, a policy could make a specific directory within a bucket public to users from a specific range of IP addresses, during particular times of the day, and only when accessing the bucket via SSL.
A bucket policy is a good way to grant public access to many objects (eg a particular directory) without having to specify permissions on each individual object. This is commonly used for static websites served out of an S3 bucket.
3. IAM Users and Groups
This is similar to defining a Bucket Policy, but permissions are assigned to specific Users or Groups of users. Thus, only those users have permission to access the objects. Users must authenticate themselves when accessing the objects, so this is most commonly used when accessing objects via the AWS API, such as using the aws s3 commands from the AWS Command-Line Interface (CLI).
Rather than being prompted to authenticate, users must provide the authentication when making the API call. A simple way of doing this is to store user credentials in a local configuration file, which the CLI will automatically use when calling the S3 API.
4. Pre-Signed URL
A Pre-Signed URL can be used to grant access to S3 objects as a way of "overriding" access controls. A normally private object can be accessed via a URL by appending an expiry time and signature. This is a great way to serve private content without requiring a web server.
Typically, an application constructs a Pre-Signed URL when it wishes to grant access to an object. For example, let's say you have a photo-sharing website and a user has authenticated to your website. You now wish to display their pictures in a web page. The pictures are normally private, but your application can generate Pre-Signed URLs that grant them temporary access to the pictures. The Pre-Signed URL will expire after a particular date/time.
Regarding the pre-signed URL, the signature is in the request headers, hence it should be within HTTPS/TLS encryption. But do check for yourself.

Restrict Access to S3 bucket on AWS

I am storing files in a S3 bucket. I want the access to the files be restricted.
Currently, anyone with the URL to the file is able to access the file.
I want a behavior where file is accessed only when it is accessed through my application. The application is hosted on EC2.
Following are 2 possible ways I could find.
Use "referer" key in bucket policy.
Change "allowed origin" in CORS configuration
Which of the above 2 should be used, given the fact that 'referer' could be spoofed in the request header.
Also can cloudfront play a role over here?
I would recommend using a Pre-Signed URL that permits access to private objects stored on Amazon S3. It is a means of keeping objects secure, yet grant temporary access to a specific object.
It is created via a hash calculation based on the object path, expiry time and a shared Secret Access Key belonging to an account that has permission to access the Amazon S3 object. The result is a time-limited URL that grants access to the object. Once the expiry time passes, the URL does not return the object.
Start by removing existing permissions that grant access to these objects. Then generate Pre-Signed URLs to grant access to private content on a per-object basis, calculated every time you reference an S3 object. (Don't worry, it's fast to do!)
See documentation: Sample code in Java
When dealing with a private S3 bucket, you'll want to use an AWS SDK appropriate for your use case.
Here lies SDKs for many different languages: http://aws.amazon.com/tools/
Within each SDK, you can find sample calls to S3.
If you are trying to make private calls via browser-side JavaScript, you can use CORS.