Skip to content

Amazon S3 Buckets Creation not properly working for regions  #40

Closed
@JattJames

Description

@JattJames

There was a discussion i saw on CFML Slack Channel where one of the Guys' named @simone has shared some details that the Creating of Buckets is not working, I replicated the cases he mentioned and noticed something, the Buckets do get Created with an error, but the Permissions are set to block access all.

Here is the Code he used for Creating Buckets

He Modified the code as below:

boolean function putBucket(
		required string bucketName    = variables.defaultBucketName,
		string acl                    = variables.defaultACL,
		string location               = "",
		string objectOwnership        = variables.defaultObjectOwnership,
		boolean BlockPublicAcls       = false,
		boolean IgnorePublicAcls      = false,
		boolean BlockPublicPolicy     = false,
		boolean RestrictPublicBuckets = false
	){
		requireBucketName( arguments.bucketName );
		if(arguments.location == "EU") {
			var constraintXML = "<CreateBucketConfiguration><LocationConstraint>EU</LocationConstraint></CreateBucketConfiguration>"
		} else if(arguments.location == 'ca-central-1') {
			var constraintXML = "<CreateBucketConfiguration><LocationConstraint>ca-central-1</LocationConstraint></CreateBucketConfiguration>"
		} else if(arguments.location == 'ap-southeast-2') {
			var constraintXML = "<CreateBucketConfiguration><LocationConstraint>ap-southeast-2</LocationConstraint></CreateBucketConfiguration>"
		} else {
			var constraintXML = "";
		}
		var headers       = { "content-type" : "text/xml" };
		if ( len( arguments.objectOwnership ) ) {
			if (
				!listFindNoCase(
					"BucketOwnerPreferred,ObjectWriter,BucketOwnerEnforced",
					arguments.objectOwnership
				)
			) {
				throw(
					message = "Invalid value [#arguments.objectOwnership#] for [objectOwnership] when creating bucket.",
					detail  = "Valid options are: [BucketOwnerPreferred, ObjectWriter, BucketOwnerEnforced]"
				);
			}
			headers[ "x-amz-object-ownership" ] = arguments.objectOwnership;
		}

		var results = s3Request(
			method   = "PUT",
			resource = arguments.bucketName,
			body     = constraintXML,
			headers  = headers
		);

		// s3 does not provide a way to set this when creating the bucket
		putBucketPublicAccess(
			arguments.bucketName,
			arguments.BlockPublicAcls,
			arguments.IgnorePublicAcls,
			arguments.BlockPublicPolicy,
			arguments.RestrictPublicBuckets
		);

		// Must set ACL in second step in case public access settings above would prevent the ACL from being saved.
		putBucketACL( arguments.bucketName, arguments.acl );

		return results.responseheader.status_code == 200;
	}

so in the above I see he added some conditions in the constraintXML which makes the code to check which region it has to choose and create

Thanks

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions