public static void initialHttpRequestMessage(OssHttpRequestMessage hReqMes, ObjectMetadata metadata) { if (metadata.CacheControl != null) { CacheControlHeaderValue temp; if (CacheControlHeaderValue.TryParse(metadata.CacheControl, out temp) == true) hReqMes.Headers.CacheControl = temp; } if (metadata.ContentDisposition != null) { ContentDispositionHeaderValue temp; if (ContentDispositionHeaderValue.TryParse(metadata.CacheControl, out temp) == true) hReqMes.Content.Headers.ContentDisposition = temp; } if (metadata.ContentLength != 0) { hReqMes.Content.Headers.ContentLength = metadata.ContentLength; } if (metadata.ContentType != null) { MediaTypeHeaderValue temp; if (MediaTypeHeaderValue.TryParse(metadata.CacheControl, out temp) == true) hReqMes.Content.Headers.ContentType = temp; else hReqMes.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream"); } else { hReqMes.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream"); } //if (metadata.ExpirationTime != null) //{ // hReqMes.Content.Headers.Expires = metadata.ExpirationTime; //} foreach (KeyValuePair<string, string> entry in metadata.UserMetadata) { hReqMes.Headers.Pragma.Add(new NameValueHeaderValue("x-oss-meta-" + entry.Key, entry.Value)); } }
static async void PutObject() { try { OssClient temp = new OssClient("v12bbAtA0rnTw0Wt", "Gz3OBVL4QHIWBBh5rD0kg1f9F4FnZU"); FileStream fs = new FileStream(@"C:\Users\zhonglei\Desktop\SLM2.m", FileMode.Open); // MemoryStream s = new MemoryStream(); ObjectMetadata oMetaData = new ObjectMetadata(); await temp.PutObject("devdoc2", "5/1.txt", fs, oMetaData, null, tokenSource.Token); fs.Dispose(); } catch(Exception ex) { } }
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(); 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; }