Oss.OssClient.PutObject C# (CSharp) Method

PutObject() public method

public PutObject ( string bucketName, string key, Stream content, ObjectMetadata metadata, Action uploadProcessCallback = null, CancellationToken cancellationToken = null ) : Task
bucketName string
key string
content Stream
metadata ObjectMetadata
uploadProcessCallback Action
cancellationToken System.Threading.CancellationToken
return Task
        public async Task<PutObjectResult> PutObject(string bucketName, string key, Stream content, ObjectMetadata metadata, 
            Action<HttpProcessData> uploadProcessCallback = null, CancellationToken? cancellationToken = null)
        {
            PutObjectResult result = null;
            HttpClientHandler hand = null;
            ProgressMessageHandler processMessageHander = null;
            HttpClient localHttpClient = null;
            OssHttpRequestMessage httpRequestMessage = null;
            HttpResponseMessage response = null;
            try
            {
                 hand = new HttpClientHandler();
                 processMessageHander = new ProgressMessageHandler(hand);
                 localHttpClient = new HttpClient(processMessageHander);
                localHttpClient.Timeout += new TimeSpan(2 * TimeSpan.TicksPerHour); 
                 httpRequestMessage = new OssHttpRequestMessage(bucketName, key);



                httpRequestMessage.Method = HttpMethod.Put;
                httpRequestMessage.Headers.Date = DateTime.UtcNow;
                httpRequestMessage.Content = new StreamContent(content);


                OssClientHelper.initialHttpRequestMessage(httpRequestMessage, metadata);

                

                OssRequestSigner.Sign(httpRequestMessage, networkCredential);

                if (uploadProcessCallback != null)
                {
                    processMessageHander.HttpSendProgress += (sender, e) =>
                    {
                        uploadProcessCallback(new HttpProcessData()
                        {
                            TotalBytes = e.TotalBytes,
                            BytesTransferred = e.BytesTransferred,
                            ProgressPercentage = e.ProgressPercentage
                        });

                    };
                }

                
                if(cancellationToken != null)
                    response = await localHttpClient.SendAsync(httpRequestMessage, (CancellationToken)cancellationToken);
                else
                     response = await localHttpClient.SendAsync(httpRequestMessage);

                if (response.IsSuccessStatusCode == false)
                {
                    await ErrorResponseHandler.Handle(response);
                }

                var temp = DeserializerFactory.GetFactory().CreatePutObjectReusltDeserializer();
                result = temp.Deserialize(response);
                //localHttpClient.Dispose();

            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (hand != null)
                    hand.Dispose();

                if (processMessageHander != null)
                    processMessageHander.Dispose();

                if (localHttpClient != null)
                    localHttpClient.Dispose();

                if (httpRequestMessage != null)
                    httpRequestMessage.Dispose();


                if (response != null)
                    response.Dispose();
            }

            return result;

        }

Usage Example

Ejemplo n.º 1
0
        public void OnFileChanged(object sender, FileSystemEventArgs e)
        {
            try
            {
                if (e.ChangeType != WatcherChangeTypes.Changed && e.ChangeType != WatcherChangeTypes.Created)
                {
                    return;
                }
                if (string.IsNullOrEmpty(Path.GetExtension(e.FullPath)))
                {
                    return;
                }

                var filePath = e.FullPath;
                var siteId   = _api.GetSiteIdByFilePath(e.FullPath);
                if (siteId <= 0)
                {
                    return;
                }
                var siteDirectoryPath = _api.GetSiteDirectoryPath(siteId);
                if (string.IsNullOrEmpty(siteDirectoryPath))
                {
                    return;
                }

                var isOss = _api.GetSiteOption(siteId, OptionIsOss) == true.ToString();
                if (!isOss)
                {
                    return;
                }

                var accessKeyId     = _api.GetSiteOption(siteId, OptionAccessKeyId);
                var accessKeySecret = _api.GetSiteOption(siteId, OptionAccessKeySecret);
                var bucketName      = _api.GetSiteOption(siteId, OptionBucketName);
                var bucketEndPoint  = _api.GetSiteOption(siteId, OptionBucketEndPoint);
                var bucketPath      = _api.GetSiteOption(siteId, OptionBucketPath);
                var key             = (bucketPath + GetRelativePath(filePath, siteDirectoryPath)).Trim('/');

                if (string.IsNullOrEmpty(accessKeyId) || string.IsNullOrEmpty(accessKeySecret) ||
                    string.IsNullOrEmpty(bucketName) || string.IsNullOrEmpty(bucketEndPoint) ||
                    string.IsNullOrEmpty(key))
                {
                    return;
                }

                var client = new OssClient(bucketEndPoint, accessKeyId, accessKeySecret);
                client.PutObject(bucketName, key, filePath);
            }
            catch (Exception ex)
            {
                _api.AddErrorLog(ex);
            }
        }
All Usage Examples Of Oss.OssClient::PutObject