public static void SyncAppendObject(string bucketName) { const string key = "AppendObject"; long position = 0; ulong initCrc = 0; try { var metadata = client.GetObjectMetadata(bucketName, key); position = metadata.ContentLength; initCrc = ulong.Parse(metadata.Crc64); } catch (Exception) {} try { using (var fs = File.Open(fileToUpload, FileMode.Open)) { var request = new AppendObjectRequest(bucketName, key) { ObjectMetadata = new ObjectMetadata(), Content = fs, Position = position, InitCrc = initCrc }; var result = client.AppendObject(request); position = result.NextAppendPosition; initCrc = result.HashCrc64Ecma; Console.WriteLine("Append object succeeded, next append position:{0}", position); } // append object by using NextAppendPosition using (var fs = File.Open(fileToUpload, FileMode.Open)) { var request = new AppendObjectRequest(bucketName, key) { ObjectMetadata = new ObjectMetadata(), Content = fs, Position = position, InitCrc = initCrc }; var result = client.AppendObject(request); position = result.NextAppendPosition; Console.WriteLine("Append object succeeded too, next append position:{0}", position); } } 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); } }
/// <summary> /// 追加上传 /// </summary> /// <param name="bucketName"></param> /// <param name="fileToUpload"></param> /// <returns></returns> public string SyncAppendObject(string bucketName, string fileToUpload) { string key = DateTime.Now.ToSerialNumber() + Path.GetExtension(fileToUpload); long position = 0; try { var metadata = client.GetObjectMetadata(bucketName, key); position = metadata.ContentLength; } catch (Exception) { } try { using (var fs = File.Open(fileToUpload, FileMode.Open)) { var request = new AppendObjectRequest(bucketName, key) { ObjectMetadata = new ObjectMetadata(), Content = fs, Position = position }; var result = client.AppendObject(request); position = result.NextAppendPosition; } // append object by using NextAppendPosition using (var fs = File.Open(fileToUpload, FileMode.Open)) { var request = new AppendObjectRequest(bucketName, key) { ObjectMetadata = new ObjectMetadata(), Content = fs, Position = position }; var result = client.AppendObject(request); position = result.NextAppendPosition; } } catch (OssException ex) { } catch (Exception ex) { } return(key); }
public void CreateOrAppend(FileId id, System.IO.Stream stream) { _OssClient.AppendObject(new AppendObjectRequest(_BucketName, id.ToString()) { Content = stream }); }
//追加文件(防止添加重复相片) public static void AppendObject() { //第一次追加文件的时候,文件可能已经存在,先获取文件的当前长度,如果不存在,position为0,如果存在则不会添加进去 long position = 0; try { var client = new OssClient("oss-cn-shenzhen.aliyuncs.com", "LTAId7dsrQHujhU5", "O3nQOqai4yXrvGCKNbvgrKuU8f7U7p"); var metadata = client.GetObjectMetadata("flowera", "2.jpg"); position = metadata.ContentLength; } catch (Exception) { } try { var client = new OssClient("oss-cn-shenzhen.aliyuncs.com", "LTAId7dsrQHujhU5", "O3nQOqai4yXrvGCKNbvgrKuU8f7U7p"); using (var fs = File.Open("D:/Users/pc/Desktop/个人文件/AlbumProject/AlbumProject/Content/2.jpg", FileMode.Open)) { var request = new AppendObjectRequest("flowera", "2.jpg") { ObjectMetadata = new ObjectMetadata(), Content = fs, Position = position }; var result = client.AppendObject(request); // 设置下次追加文件时的position位置 position = result.NextAppendPosition; Console.WriteLine("Append object succeeded, next append position:{0}", position); } // 再次追加文件,这时候的position值可以从上次的结果中获取到 using (var fs = File.Open("D:/Users/pc/Desktop/个人文件/AlbumProject/AlbumProject/Content/2.jpg", FileMode.Open)) { var request = new AppendObjectRequest("flowera", "2.jpg") { ObjectMetadata = new ObjectMetadata(), Content = fs, Position = position }; var result = client.AppendObject(request); position = result.NextAppendPosition; Console.WriteLine("Append object succeeded, next append position:{0}", position); } } catch (Exception ex) { Console.WriteLine("Append object failed, {0}", ex.Message); } }
public static void AppendObjectProgress(string bucketName) { const string key = "AppendObjectProgress"; long position = 0; try { using (var fs = File.Open(fileToUpload, FileMode.Open)) { var request = new AppendObjectRequest(bucketName, key) { ObjectMetadata = new ObjectMetadata(), Content = fs, Position = position }; request.StreamTransferProgress += streamProgressCallback; var result = client.AppendObject(request); position = result.NextAppendPosition; Console.WriteLine("Append object succeeded, next append position:{0}", position); } } 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); } finally { client.DeleteObject(bucketName, key); } }