LitS3.S3Service.QueryBucket C# (CSharp) Method

QueryBucket() public method

Queries S3 about the existance and ownership of the given bucket name.
public QueryBucket ( string bucketName ) : BucketAccess
bucketName string
return BucketAccess
        public BucketAccess QueryBucket(string bucketName)
        {
            try
            {
                // recommended technique from amazon: try and list contents of the bucket with 0 maxkeys
                var args = new ListObjectsArgs { MaxKeys = 0 };
                new ListObjectsRequest(this, bucketName, args).GetResponse().Close();

                return BucketAccess.Accessible;
            }
            catch (S3Exception exception)
            {
                switch (exception.ErrorCode)
                {
                    case S3ErrorCode.NoSuchBucket: return BucketAccess.NoSuchBucket;
                    case S3ErrorCode.AccessDenied: return BucketAccess.NotAccessible;
                    default: throw;
                }
            }
        }

Usage Example

Example #1
0
        public S3Store()
        {
            _S3Service = new S3Service()
            {
                AccessKeyID = System.Environment.GetEnvironmentVariable("AMAZON_ACCESS_KEY_ID"),
                SecretAccessKey = System.Environment.GetEnvironmentVariable("AMAZON_SECRET_ACCESS_KEY")
            };

            if (_S3Service.QueryBucket(_bucketName) == BucketAccess.NoSuchBucket)
                _S3Service.CreateBucket(_bucketName);
        }