/// <summary> /// 异步上传object /// </summary> /// <param name="bucketName"></param> /// <param name="key">上传名(path/filename or path/)带反斜线的默认上传为目录,否则上传为文件</param> /// <param name="fileName">本地上传文件名</param> public void AsyncPutObject(string bucketName, string key, string fileName) { try { using (var fs = File.Open(fileName, FileMode.Open)) { var metadata = new ObjectMetadata(); metadata.UserMetadata.Add("mykey1", "myval1"); metadata.UserMetadata.Add("mykey2", "myval2"); metadata.CacheControl = "No-Cache"; metadata.ContentType = "text/html"; var result = "Notice user: put object finish"; _client.BeginPutObject(bucketName, key, fs, metadata, PutObjectCallback, result.ToCharArray()); _event.WaitOne(); } } catch (OssException ex) { Console.WriteLine("Failed with error code: {0}; Error info: {1}. \nRequestID:{2}\tHostID:{3}", ex.ErrorCode, ex.Message, ex.RequestId, ex.HostId); } catch (Exception ex) { Console.WriteLine("Failed with error info: {0}", ex.Message); } }
public static void AsyncPutObjectProgress(string bucketName) { const string key = "AsyncPutObjectProgress"; try { using (var fs = File.Open(fileToUpload, FileMode.Open)) { var putObjectRequest = new PutObjectRequest(bucketName, key, fs); putObjectRequest.StreamTransferProgress += streamProgressCallback; string result = "Notice user: put object finish"; client.BeginPutObject(putObjectRequest, PutObjectCallback, result.ToCharArray()); _event.WaitOne(); } } catch (OssException ex) { Console.WriteLine("Failed with error code: {0}; Error info: {1}. \nRequestID:{2}\tHostID:{3}", ex.ErrorCode, ex.Message, ex.RequestId, ex.HostId); } catch (Exception ex) { Console.WriteLine("Failed with error info: {0}", ex.Message); } }
public static void AsyncPutObject(string bucketName, string filename, Action <string> putCallback = null) { CreateDispatcherGameObject(); try { var fs = new FileStream(filename, FileMode.Open); string md5 = OssUtils.ComputeContentMd5(fs, fs.Length); var ossClient = new OssClient(endPoint, stsToken.AccessKeyId, stsToken.AccessKeySecret, stsToken.SecurityToken); var metadata = new ObjectMetadata(); metadata.ContentLength = fs.Length; metadata.CacheControl = "public"; metadata.ContentMd5 = md5; var filemd5 = Utils.File.FormatMD5(Convert.FromBase64String(md5)); string ext = System.IO.Path.GetExtension(filename); var ossFileName = filemd5 + ext; Hashtable state = new Hashtable(); state["client"] = ossClient; state["fs"] = fs; state["callback"] = putCallback; state["ossfilename"] = ossFileName; ossClient.BeginPutObject(bucketName, ossFileName, fs, metadata, PutObjectCallback, state); // _event.WaitOne(); } catch (OssException ex) { Console.WriteLine("Failed with error code: {0}; Error info: {1}. \nRequestID:{2}\tHostID:{3}", ex.ErrorCode, ex.Message, ex.RequestId, ex.HostId); if (putCallback != null) { if (ex is OssException) { putCallback(((OssException)ex).ErrorCode); } else { putCallback(ex.Message); } } } catch (Exception ex) { Console.WriteLine("Failed with error info: {0}", ex.Message); if (putCallback != null) { if (ex is ServiceException) { putCallback(((ServiceException)ex).ErrorCode); } else { putCallback(ex.Message); } } } }
public static void AsyncPutObject() { try { // 1. put object to specified output stream var fs = File.Open(fileToUpload, FileMode.Open); var metadata = new ObjectMetadata(); metadata.UserMetadata.Add("mykey1", "myval1"); metadata.UserMetadata.Add("mykey2", "myval2"); metadata.CacheControl = "No-Cache"; metadata.ContentType = "text/html"; client.BeginPutObject(bucketName, key, fs, metadata, PutObjectCallback, new string('a', 8)); _evnet.WaitOne(); fs.Close(); } catch (OssException ex) { Console.WriteLine("Failed with error code: {0}; Error info: {1}. \nRequestID:{2}\tHostID:{3}", ex.ErrorCode, ex.Message, ex.RequestId, ex.HostId); } catch (Exception ex) { Console.WriteLine("Failed with error info: {0}", ex.Message); } }
public static void UploadFile(string localPath, string buckName, string key) { bool waitForResult = true; if (!File.Exists(localPath)) { Debug.LogError("File path not exist: " + localPath); } using (var fs = File.Open(localPath, FileMode.Open)) { OssClient ossClient = new OssClient(Config.Endpoint, Config.AccessKeyId, Config.AccessKeySecret); try { var metadata = new ObjectMetadata(); metadata.ContentLength = fs.Length; metadata.CacheControl = "public"; ossClient.BeginPutObject(buckName, key, fs, metadata, (asyncResult) => { try { ossClient.EndPutObject(asyncResult); } catch (Exception ex) { Debug.LogError(ex); } finally { if (OnUploaded != null) { OnUploaded.Invoke(); } waitForResult = false; } }, null); } catch (OssException ex) { Debug.LogError(string.Format("Failed with error code: {0}; Error info: {1}. \nRequestID:{2}\tHostID:{3}", ex.ErrorCode, ex.Message, ex.RequestId, ex.HostId)); waitForResult = false; } catch (Exception ex) { Debug.LogError(ex); waitForResult = false; } while (waitForResult) { if (OnUploading != null) { OnUploading.Invoke(); } } } }
/// <summary> /// /// </summary> /// <param name="stream"></param> /// <param name="FileName"></param> public void AliyunFileUpLoadAsync(Stream stream, string FileName) { using (stream) { // 增加自定义元信息。 var metadata = new ObjectMetadata(); Client.BeginPutObject( bucketName: Final.BucketName, key: FileName, content: stream, callback: AliyunFileUpLoadCallBack, state: "OK" ); _event.WaitOne(); } }
public static void AsyncPutObject() { var client = new OssClient("oss-cn-shenzhen.aliyuncs.com", "LTAId7dsrQHujhU5", "O3nQOqai4yXrvGCKNbvgrKuU8f7U7p"); try { using (var fs = File.Open("D:/Users/pc/Desktop/个人文件/AlbumProject/AlbumProject/Content/2.jpg", FileMode.Open)) { var metadata = new ObjectMetadata(); metadata.CacheControl = "No-Cache"; metadata.ContentType = "text/html"; client.BeginPutObject("flowera", "pic.jpg", fs, metadata, PutObjectCallback, new string('a', 8)); _event.WaitOne(); } } catch (Exception ex) { Console.WriteLine("Put object failed, {0}", ex.Message); } }
//TODO //PutFileWithHeader() /// <summary> /// Pub File Async /// </summary> /// <param name="bucketName">Bucket Name</param> public void PutFileAsync(string bucketName, string filePath, string objcetKey, Action act) { try { resetEvent.Reset(); using (var fs = File.Open(filePath, FileMode.Open)) { string result = "Notice user: put object finish"; ObjectMetadata metadata = new ObjectMetadata(); client.BeginPutObject(bucketName, objcetKey, fs, metadata, PutFileAsyncCallback, result.ToCharArray()); resetEvent.WaitOne(); if (act != null) { act(); } } } catch (OssException ex) { lastError = ex; } }
public void AsyncPutObject(string key, Stream fileContent) { _oss.BeginPutObject(ImageBucket, key, fileContent, PutObjectCallback, "sintoacct"); _event.WaitOne(); }
public async Task <bool> SaveFileAsync(string path, Stream stream, CancellationToken cancellationToken = default) { if (String.IsNullOrEmpty(path)) { throw new ArgumentNullException(nameof(path)); } if (stream == null) { throw new ArgumentNullException(nameof(stream)); } var seekableStream = stream.CanSeek ? stream : new MemoryStream(); if (!stream.CanSeek) { await stream.CopyToAsync(seekableStream).AnyContext(); seekableStream.Seek(0, SeekOrigin.Begin); } try { var putResult = await Task.Factory.FromAsync((request, callback, state) => _client.BeginPutObject(request, callback, state), result => _client.EndPutObject(result), new PutObjectRequest(_bucket, NormalizePath(path), seekableStream), null).AnyContext(); return(putResult.HttpStatusCode == HttpStatusCode.OK); } catch (Exception) { return(false); } finally { if (!stream.CanSeek) { seekableStream.Dispose(); } } }