How to Check If a Resource Exists in an Aws S3Bucket

How to check if a resource exists in an AWS S3bucket

#exists? ⇒ Boolean

Returns true if the object exists in S3.

# new object, does not exist yet
obj = bucket.objects["my-text-object"]
# no instruction file present
begin
bucket.objects['my-text-object.instruction'].exists? #=> false
rescue
# exists? can raise an error `Aws::S3::Errors::Forbidden`
end

# store the encryption materials in the instruction file
# instead of obj#metadata
obj.write("MY TEXT",
:encryption_key => MY_KEY,
:encryption_materials_location => :instruction_file)

begin
bucket.objects['my-text-object.instruction'].exists? #=> true
rescue
# exists? can raise an error `Aws::S3::Errors::Forbidden`
end

http://docs.aws.amazon.com/AWSRubySDK/latest/AWS/S3/S3Object.html#exists%3F-instance_method

How to check if an object exists or filename matches in s3 bucket?

If you know the exact key

response = client.head_object(
Bucket='examplebucket',
Key='HappyFace.jpg',
)

If no such object matches the key, S3.Client.exceptions.NoSuchKey will be thrown. The object data is not transfered to your local, if that's your concern

How to check if specific resource already exists in CloudFormation script

There is no obvious way to do this, unless you create the template dynamically with an explicit check. Stacks created from the same template are independent entities, and if you create a stack that contains a bucket, delete the stack while retaining the bucket, and then create a new stack (even one with the same name), there is no connection between this new stack and the bucket created as part of the previous stack.

If you want to use the same S3 bucket for multiple stacks (even if only one of them exists at a time), that bucket does not really belong in the stack - it would make more sense to create the bucket in a separate stack, using a separate template (putting the bucket URL in the "Outputs" section), and then referencing it from your original stack using a parameter.

Update November 2019:

There is a possible alternative now. On Nov 13th AWS launched CloudFormation Resource Import. With that feature you can now creating a stack from existing resources. Currently not many resource types are supported by this feature, but S3 buckets are.

In your case you'd have to do it in two steps:

  1. Create a template that only contains the preexisting S3 bucket using the "Create stack" > "With existing resources (import resources)" (this is the --change-set-type IMPORT flag in the CLI) (see docs)
  2. Update the the template to include all resources that don't already exist.

As they note in their documentation; this feature is very versatile. So it opens up a lot of possibilities. See docs for more info.

How to check if a specified key exists in a given S3 bucket using Java

Use the jets3t library. Its a lot more easier and robust than the AWS sdk. Using this library you can call, s3service.getObjectDetails(). This will check and retrieve only the details of the object (not the contents) of the object. It will throw a 404 if the object is missing. So you can catch that exception and deal with it in your app.

But in order for this to work, you will need to have ListBucket access for the user on that bucket. Just GetObject access will not work. The reason being, Amazon will prevent you from checking for the presence of the key if you dont have ListBucket access. Just knowing whether a key is present or not, will also suffice for malicious users in some cases. Hence unless they have ListBucket access they will not be able to do so.

How do I test if a bucket exists on AWS S3

You can use the following code:

// import or require aws-sdk as AWS
// const AWS = require('aws-sdk');

const checkBucketExists = async bucket => {
const s3 = new AWS.S3();
const options = {
Bucket: bucket,
};
try {
await s3.headBucket(options).promise();
return true;
} catch (error) {
if (error.statusCode === 404) {
return false;
}
throw error;
}
};

The important thing is to realize that the error statusCode will be 404 if the bucket does not exist.

Determine if an object exists in a S3 bucket based on wildcard

Using the AWSSDK For .Net I Currently do something along the lines of:

public bool Exists(string fileKey, string bucketName)
{
try
{
response = _s3Client.GetObjectMetadata(new GetObjectMetadataRequest()
.WithBucketName(bucketName)
.WithKey(key));

return true;
}

catch (Amazon.S3.AmazonS3Exception ex)
{
if (ex.StatusCode == System.Net.HttpStatusCode.NotFound)
return false;

//status wasn't not found, so throw the exception
throw;
}
}

It kinda sucks, but it works for now.

How can I easily determine if a Boto 3 S3 bucket resource exists?

At the time of this writing there is no high-level way to quickly check whether a bucket exists and you have access to it, but you can make a low-level call to the HeadBucket operation. This is the most inexpensive way to do this check:

from botocore.client import ClientError

try:
s3.meta.client.head_bucket(Bucket=bucket.name)
except ClientError:
# The bucket does not exist or you have no access.

Alternatively, you can also call create_bucket repeatedly. The operation is idempotent, so it will either create or just return the existing bucket, which is useful if you are checking existence to know whether you should create the bucket:

bucket = s3.create_bucket(Bucket='my-bucket-name')

As always, be sure to check out the official documentation.

Note: Before the 0.0.7 release, meta was a Python dictionary.



Related Topics



Leave a reply



Submit