Example #1
0
        static async Task PutObjectToS3Async(BucketCredentials credentials, BucketProperties bucketProperties)
        {
            try
            {
                var client     = new AmazonS3Client(credentials.AccessKeyId, credentials.SecretAccessKey, credentials.SessionToken, bucketProperties.BucketRegion);
                var putRequest = new PutObjectRequest
                {
                    BucketName = bucketProperties.BucketName,
                    Key        = bucketProperties.BucketObjectKey,
                    FilePath   = bucketProperties.BucketFilePath,
                };

                await client.PutObjectAsync(putRequest);

                Console.WriteLine($"Successfully put {bucketProperties.BucketObjectKey} to {bucketProperties.BucketName}");
            }
            catch (AmazonS3Exception e)
            {
                Console.WriteLine(
                    "Error encountered ***. Message:'{0}' when writing an object"
                    , e.Message);
            }
            catch (Exception e)
            {
                Console.WriteLine(
                    "Unknown encountered on server. Message:'{0}' when writing an object"
                    , e.Message);
            }
        }
Example #2
0
        private static void SuccessfulScenarioUploadingToS3(BucketProperties bucketProperties)
        {
            // Successfully uploads file, after assuming given role

            Action <AssumeRoleResponse> putObjectToBucket = (assumeRoleResponse) =>
            {
                PutObjectToS3Async(assumeRoleResponse, bucketProperties).Wait();
            };

            RoleManagement.AssumeGivenRole(_awsOptions, _roleArn, _roleSessionName, putObjectToBucket).Wait();
        }
Example #3
0
        public static void Main()
        {
            var bucketProperties = new BucketProperties
            {
                BucketName      = "test.eventz.com",
                BucketObjectKey = "test-file",
                BucketFilePath  = @"C:\Code\sports-form\test-file-chery.txt",
                BucketRegion    = RegionEndpoint.APSoutheast2
            };

            Utils.ReadInputAndAction(() => SuccessfulScenarioUploadingToS3(bucketProperties),
                                     () => FailureScenarioUploadingToS3(bucketProperties));
        }
Example #4
0
        private static void FailureScenarioUploadingToS3(BucketProperties bucketProperties)
        {
            // Fails with following error
            // Error encountered ***. Message:'The provided token is malformed or otherwise invalid.' when writing an object


            var bucketCredentials = new BucketCredentials
            {
                AccessKeyId     = "AKIAJ42OR63QAZ7REWNQ",                     // replace with the correct access key
                SecretAccessKey = "KjdlCjUf2Vag7sPtRGpqZjzvZfKbh5CRKbDBRxVy", // replace with correct secret key
                SessionToken    = _roleSessionName
            };

            PutObjectToS3Async(bucketCredentials, bucketProperties).Wait();
        }
Example #5
0
        /// <summary>
        /// Adds client to current pool
        /// </summary>
        /// <param name="bucket">Name of the bucket to add.</param>
        /// <param name="password">Password of the bucket.</param>
        /// <returns>True if bucket was added.</returns>
        public bool AddBucketClient(string bucket, string password)
        {
            BucketProperties propItem = new BucketProperties();

            if (this._client == null)
            {
                this._client = new ConcurrentDictionary<string, CouchbaseClient>();
                this._bucketProps = new ConcurrentDictionary<string, BucketProperties>();

                if (bucket == null)
                {
                    if (!this._client.TryAdd(MembasePool.DefaultPool, new CouchbaseClient()))
                        return false;

                    propItem.BucketName = MembasePool.DefaultPool;
                    propItem.BucketPassword = "";
                    this._bucketProps.TryAdd(propItem.BucketName, propItem);
                }
                else
                {
                    if (!this._client.TryAdd(bucket, new CouchbaseClient(bucket, password)))
                        return false;

                    propItem.BucketName = bucket;
                    propItem.BucketPassword = password;
                    this._bucketProps.TryAdd(propItem.BucketName, propItem);
                }
            }
            else
            {
                if (bucket == null && !this._client.ContainsKey(MembasePool.DefaultPool))
                {
                    if (!this._client.TryAdd(MembasePool.DefaultPool, new CouchbaseClient()))
                        return false;

                    propItem.BucketName = MembasePool.DefaultPool;
                    propItem.BucketPassword = "";
                    this._bucketProps.TryAdd(propItem.BucketName, propItem);
                }
                else if (!this._client.ContainsKey(bucket))
                {
                    if (!this._client.TryAdd(bucket, new CouchbaseClient(bucket, password)))
                        return false;

                    propItem.BucketName = bucket;
                    propItem.BucketPassword = password;
                    this._bucketProps.TryAdd(propItem.BucketName, propItem);
                }
                else
                    return false;
            }

            return true;
        }
Example #6
0
        /// <exception cref="ArgumentNullException">Null exception</exception>
        /// <summary>
        /// Instantiates a new membase pool with custom recycle period. Heavy object!
        /// </summary>
        /// <param name="bucket">Bucket to connect to.</param>
        /// <param name="password">Password for the bucket.</param>
        /// <param name="maximumClientUptime">Sets maximum uptime for pool before it recycles.</param>
        public MembasePool(string bucket, string password, TimeSpan maximumClientUptime)
        {
            this._client = new ConcurrentDictionary<string, CouchbaseClient>();
            this._lastRecycle = DateTime.Now;
            this.RecycleAfter = maximumClientUptime;

            BucketProperties propItem = new BucketProperties();

            if (bucket == null)
            {
                this._client.TryAdd(MembasePool.DefaultPool, new CouchbaseClient());
                propItem.BucketName = MembasePool.DefaultPool;
                propItem.BucketPassword = "";
            }
            else
            {
                this._client.TryAdd(bucket, new CouchbaseClient(bucket, password));
                propItem.BucketName = bucket;
                propItem.BucketPassword = password;
            }

            this._bucketProps = new ConcurrentDictionary<string, BucketProperties>();
            this._bucketProps.TryAdd(propItem.BucketName, propItem);
        }
Example #7
0
        public bool SetBucketProperties(string bucket, BucketProperties properties)
        {
            var command = CommandFactory.CreateSetBucketProperties(bucket, properties);

            return((command.Execute()) != null);
        }