public static void AsynPostObject(COSXML.CosXml cosXml, string bucket, string key, string srcPath, PostObjectRequest.Policy policy) { PostObjectRequest request = new PostObjectRequest(bucket, key, srcPath); //设置签名有效时长 request.SetSign(TimeUtils.GetCurrentTime(TimeUnit.SECONDS), 600); request.SetCosProgressCallback(delegate(long completed, long total) { Console.WriteLine(String.Format("progress = {0} / {1} : {2:##.##}%", completed, total, completed * 100.0 / total)); }); //设置policy request.SetPolicy(policy); //执行请求 cosXml.PostObject(request, delegate(CosResult result) { PostObjectResult getObjectResult = result as PostObjectResult; Console.WriteLine(getObjectResult.GetResultInfo()); }, delegate(CosClientException clientEx, CosServerException serverEx) { if (clientEx != null) { QLog.D("XIAO", clientEx.Message); Console.WriteLine("CosClientException: " + clientEx.StackTrace); } if (serverEx != null) { QLog.D("XIAO", serverEx.Message); Console.WriteLine("CosServerException: " + serverEx.GetInfo()); } }); }
public static void PostObject(COSXML.CosXml cosXml, string bucket, string key, string srcPath, PostObjectRequest.Policy policy) { try { PostObjectRequest request = new PostObjectRequest(bucket, key, srcPath); //设置签名有效时长 //request.SetSign(TimeUtils.GetCurrentTime(TimeUnit.SECONDS), 600); List <string> headers = new List <string>(); headers.Add("Host"); request.SetSign(TimeUtils.GetCurrentTime(TimeUnit.SECONDS), 600, headers, null); request.SetCosProgressCallback(delegate(long completed, long total) { Console.WriteLine(String.Format("progress = {0} / {1} : {2:##.##}%", completed, total, completed * 100.0 / total)); }); //设置policy request.SetPolicy(policy); //执行请求 PostObjectResult result = cosXml.PostObject(request); Console.WriteLine(result.GetResultInfo()); } catch (COSXML.CosException.CosClientException clientEx) { QLog.D("XIAO", clientEx.Message); Console.WriteLine("CosClientException: " + clientEx.StackTrace); } catch (COSXML.CosException.CosServerException serverEx) { QLog.D("XIAO", serverEx.Message); Console.WriteLine("CosServerException: " + serverEx.GetInfo()); } }
public void testPostObjectTrafficLimit() { try { long now = new DateTimeOffset(DateTime.UtcNow).ToUnixTimeMilliseconds(); PostObjectRequest request = new PostObjectRequest(bucket, commonKey, smallFileSrcPath); request.LimitTraffic(8 * 1000 * 1024); //执行请求 PostObjectResult result = cosXml.PostObject(request); long costTime = new DateTimeOffset(DateTime.UtcNow).ToUnixTimeMilliseconds() - now; Console.WriteLine("costTime = " + costTime + "ms"); Console.WriteLine(result.GetResultInfo()); Assert.True(result.httpCode == 204); // Assert.True(costTime > 8000 && costTime < 14000); } catch (COSXML.CosException.CosClientException clientEx) { Console.WriteLine("CosClientException: " + clientEx.Message); Assert.True(false); } catch (COSXML.CosException.CosServerException serverEx) { Console.WriteLine("CosServerException: " + serverEx.GetInfo()); Assert.True(false); } }
public void PostObject() { try { PostObjectRequest request = new PostObjectRequest(bucket, commonKey, smallFileSrcPath); List <string> headers = new List <string>(); headers.Add("Host"); request.SetCosProgressCallback(delegate(long completed, long total) { Console.WriteLine(String.Format("progress = {0} / {1} : {2:##.##}%", completed, total, completed * 100.0 / total)); }); //设置policy request.SetPolicy(null); //执行请求 PostObjectResult result = cosXml.PostObject(request); Console.WriteLine(result.GetResultInfo()); } catch (COSXML.CosException.CosClientException clientEx) { Console.WriteLine("CosClientException: " + clientEx.Message); Assert.True(false); } catch (COSXML.CosException.CosServerException serverEx) { Console.WriteLine("CosServerException: " + serverEx.GetInfo()); Assert.True(false); } }
/// POST 方式上传对象 public void PostObject() { //.cssg-snippet-body-start:[post-object] try { string bucket = "examplebucket-1250000000"; //存储桶,格式:BucketName-APPID string key = "exampleobject"; //对象键 string srcPath = @"temp-source-file"; //本地文件绝对路径 PostObjectRequest request = new PostObjectRequest(bucket, key, srcPath); //设置进度回调 request.SetCosProgressCallback(delegate(long completed, long total) { Console.WriteLine(String.Format("progress = {0:##.##}%", completed * 100.0 / total)); }); //执行请求 PostObjectResult result = cosXml.PostObject(request); //请求成功 Console.WriteLine(result.GetResultInfo()); } catch (COSXML.CosException.CosClientException clientEx) { //请求失败 Console.WriteLine("CosClientException: " + clientEx); } catch (COSXML.CosException.CosServerException serverEx) { //请求失败 Console.WriteLine("CosServerException: " + serverEx.GetInfo()); } //.cssg-snippet-body-end }
public void AddTencentCos(string path, OnProgressCallback postpath, string name) { CosXmlConfig config = new CosXmlConfig.Builder() .SetConnectionTimeoutMs(SetConnectionTimeoutMs) //设置连接超时时间,单位毫秒,默认45000ms .SetReadWriteTimeoutMs(SetReadWriteTimeoutMs) //设置读写超时时间,单位毫秒,默认45000ms .IsHttps(IsHttps) //设置默认 HTTPS 请求 .SetAppid(SetAppid) //设置腾讯云账户的账户标识 APPID .SetRegion(SetRegion) //设置一个默认的存储桶地域 .Build(); string secretId = SecretId; //云 API 密钥 SecretId string secretKey = SecretKey; //云 API 密钥 SecretKey long durationSecond = 60000; //每次请求签名有效时长,单位为秒 QCloudCredentialProvider qCloudCredentialProvider = new DefaultQCloudCredentialProvider(secretId, secretKey, durationSecond); CosXml cosXml = new CosXmlServer(config, qCloudCredentialProvider); try { string bucket = "yuanguhl"; //存储桶,格式:BucketName-APPID string key = $"{DateTime.Now.ToString("yy")}/Add/" + name; //对象在存储桶中的位置,即称对象键 string srcPath = path; //本地文件绝对路径 if (!File.Exists(srcPath)) { // 如果不存在目标文件,创建一个临时的测试文件 File.WriteAllBytes(srcPath, new byte[1024]); } PostObjectRequest request = new PostObjectRequest(bucket, key, srcPath); request.Region = "ap-beijing"; //设置签名有效时长 request.SetSign(TimeUtils.GetCurrentTime(TimeUnit.SECONDS), 600); //设置进度回调 request.SetCosProgressCallback(postpath); //执行请求 PostObjectResult result = cosXml.PostObject(request); //请求成功 } catch (COSXML.CosException.CosClientException clientEx) { //请求失败 throw clientEx; } catch (COSXML.CosException.CosServerException serverEx) { //请求失败 throw serverEx; } }
public async Task <bool> PutObjectAsync(string bucketName, string objectName, Stream data, CancellationToken cancellationToken = default) { byte[] StreamToBytes(Stream stream) { byte[] bytes = new byte[stream.Length]; stream.Read(bytes, 0, bytes.Length); // 设置当前流的位置为流的开始 stream.Seek(0, SeekOrigin.Begin); return(bytes); } if (string.IsNullOrEmpty(bucketName)) { throw new ArgumentNullException(nameof(bucketName)); } if (string.IsNullOrEmpty(objectName)) { throw new ArgumentNullException(nameof(objectName)); } byte[] upload = StreamToBytes(data); if (upload == null || upload.Length == 0) { throw new Exception("Upload file stram is null."); } string contentType = "application/octet-stream"; if (data is FileStream fileStream) { string fileName = fileStream.Name; if (!string.IsNullOrEmpty(fileName)) { new FileExtensionContentTypeProvider().TryGetContentType(fileName, out contentType); } } if (string.IsNullOrEmpty(contentType)) { contentType = "application/octet-stream"; } bucketName = ConvertBucketName(bucketName); PostObjectRequest request = new PostObjectRequest(bucketName, objectName, upload); request.SetContentType(contentType); PostObjectResult result = _client.PostObject(request); return(await Task.FromResult(result.IsSuccessful())); }