Ejemplo n.º 1
0
        public async Task PutAsync(IBlob blob, PutBlobOptions options)
        {
            Ensure.NotNull(blob, nameof(blob));

            if (options?.EncryptionKey != null)
            {
                throw new Exception("Encryption keys not yet supported");
            }

            var stream = await blob.OpenAsync();

            await client.PutObjectAsync(new PutObjectRequest(name, blob.Key, stream, blob.Properties));
        }
Ejemplo n.º 2
0
        public async Task PutAsync(IBlob blob, PutBlobOptions options)
        {
            #region Preconditions

            if (blob == null)
            {
                throw new ArgumentNullException(nameof(blob));
            }

            if (blob.Name == null)
            {
                throw new ArgumentNullException("blob.Name");
            }

            #endregion

            // TODO: Chunked upload

            var stream = await blob.OpenAsync().ConfigureAwait(false);

            #region Stream conditions

            if (stream.Length == 0)
            {
                throw new ArgumentException("May not be empty", nameof(blob));
            }

            // Ensure we're at the start of the stream
            if (stream.CanSeek && stream.Position != 0)
            {
                throw new ArgumentException($"Must be 0. Was {stream.Position}.", paramName: "blob.Position");
            }

            #endregion

            var request = new PutObjectRequest(client.Host, bucketName, blob.Name);

            request.SetStream(stream);

            // Server side encrpytion
            if (options.EncryptionKey != null)
            {
                request.SetCustomerEncryptionKey(new ServerSideEncryptionKey(options.EncryptionKey));
            }

            SetHeaders(request, blob.Metadata);

            await client.PutObjectAsync(request).ConfigureAwait(false);
        }
Ejemplo n.º 3
0
        public async Task PutAsync(IBlob blob, PutBlobOptions options)
        {
            if (blob is null)
            {
                throw new ArgumentNullException(nameof(blob));
            }

            if (blob.Key is null)
            {
                throw new ArgumentNullException("blob.Key");
            }

            // TODO: Chunked upload

            Stream stream = await blob.OpenAsync().ConfigureAwait(false);

            #region Stream conditions

            if (stream.Length == 0)
            {
                throw new ArgumentException("May not be empty", nameof(blob));
            }

            // Ensure we're at the start of the stream
            if (stream.CanSeek && stream.Position != 0)
            {
                throw new ArgumentException($"Must be 0. Was {stream.Position}.", paramName: "blob.Position");
            }

            #endregion

            int retryCount = 0;

            Exception lastException;

            do
            {
                var request = new PutObjectRequest(client.Host, bucketName, blob.Key);

                request.SetStream(stream);

                // Server side encrpytion
                if (options.EncryptionKey != null)
                {
                    request.SetCustomerEncryptionKey(new ServerSideEncryptionKey(options.EncryptionKey));
                }

                SetHeaders(request, blob.Properties);

                try
                {
                    await client.PutObjectAsync(request).ConfigureAwait(false);

                    return;
                }
                catch (S3Exception ex) when(ex.IsTransient)
                {
                    lastException = ex;
                }

                stream.Position = 0; // reset the stream position

                retryCount++;

                await Task.Delay(retryPolicy.GetDelay(retryCount)).ConfigureAwait(false);
            }while (retryPolicy.ShouldRetry(retryCount));

            throw lastException;
        }