Beispiel #1
0
        /// <summary>
        /// Save binary object
        /// </summary>
        /// <param name="objectName">Object name</param>
        /// <param name="objectData">Object byte array</param>
        /// <param name="contentType">Content type - Optional (Used for PDF and text files to directly show in browser when issuing temporary link)</param>
        /// <param name="metaData">Object meta data</param>
        /// <param name="bucketName">Bucket name - Optional if passed throuhg constructor</param>
        /// <returns>Object Id</returns>
        /// <exception cref="EndpointUnreachableException">Thrown when S3 endpoint is unreachable.</exception>
        /// <exception cref="S3BaseException">Thrown when exception is not handled.</exception>
        public async Task <string> SaveObject(string objectName, byte[] objectData, string contentType = null, Dictionary <string, string> metaData = null, string bucketName = null)
        {
            var bucket = bucketName?.ToLower() ?? _bucketName;

            Validation.ValidateBucketName(bucket);
            Validation.ValidateObjectName(objectName);

            //var s3Obj = BucketHelper.ExtractObjectInfo(objectName);
            //var bucket = s3Obj.bucketName?.ToLower() ?? _bucketName;
            //var objectName = s3Obj.objectName;

            if (objectName.StartsWith(bucket))
            {
                objectName = objectName.Replace($"{bucket}/", "");
            }

            try
            {
                await _requestRetryPolicy.ExecuteAsync(async() =>
                {
                    if (objectData != null)
                    {
                        using (var ms = new MemoryStream(objectData))
                        {
                            await _objectOperationsClient.PutObjectAsync(bucket, objectName, data: ms, size: ms.Length, contentType: contentType, metaData: metaData);
                        }
                    }
                    else
                    {
                        await _objectOperationsClient.PutObjectAsync(bucket, objectName, data: null, size: 0, contentType: contentType, metaData: metaData);
                    }
                });
            }
            catch (MinioException ex) when(ex is ConnectionException ||
                                           ex is InternalServerException ||
                                           ex is InternalClientException ||
                                           ex is InvalidEndpointException
                                           )
            {
                throw new EndpointUnreachableException(_endpoint, ex.ToString());
            }
            catch (S3BaseException)
            {
                throw;
            }
            catch (Exception ex)
            {
                throw new S3BaseException(ex.Message, ex.ToString());
            }

            return($"{bucket}/{objectName}");
        }
Beispiel #2
0
        public Task <PutObjectResponse> UploadAsync(Stream data, CancellationToken token = default)
        {
            _request.Method  = HttpMethod.PUT;
            _request.Content = data;

            return(_objectOperations.PutObjectAsync(_request, token));
        }
Beispiel #3
0
    public Task <PutObjectResponse> PutObjectAsync(string bucketName, string objectKey, Stream?data, Action <PutObjectRequest>?config = null, CancellationToken token = default)
    {
        PutObjectRequest req = new PutObjectRequest(bucketName, objectKey, data);

        config?.Invoke(req);

        return(_objectOperations.PutObjectAsync(req, token));
    }