全部产品
云市场

示例

更新时间:2017-10-17 17:52:37

下面代码演示如何使用智能云相册的.Net SDK来完成照片上传、获取照片缩略图和删除照片等基本功能。请参考代码中的注释填写相关配置。

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using Aliyun.Acs.Core;
  7. using Aliyun.Acs.Core.Profile;
  8. using Aliyun.Acs.CloudPhoto.Model.V20170711;
  9. using Aliyun.Acs.Core.Http;
  10. using System.IO;
  11. using System.Security.Cryptography;
  12. using Aliyun.OSS;
  13. namespace ConsoleApp1
  14. {
  15. class Program
  16. {
  17. // 具有访问智能云相册权限的AK,可以是主账号的AK,也可以是授权后的子账号AK。
  18. static string ACCESS_KEY_ID = "";
  19. static string ACCESS_KEY_SECRET = "";
  20. // 修改为您的StoreName和照片库Id
  21. static string STORE_NAME = "";
  22. static string LID_ID = "";
  23. // 本地待上传文件的路径
  24. static string FILE_TO_UPLOAD = "";
  25. static void Main(string[] args)
  26. {
  27. // 构建Client对象
  28. IClientProfile profile = DefaultProfile.GetProfile("cn-shanghai", ACCESS_KEY_ID, ACCESS_KEY_SECRET);
  29. DefaultAcsClient client = new DefaultAcsClient(profile);
  30. // 上传照片
  31. long id = UploadPhoto(client, FILE_TO_UPLOAD);
  32. // 获取照片的访问地址
  33. GetAccessUrl(client, id);
  34. Console.WriteLine("Press any key to continue.");
  35. Console.Read();
  36. // 删除照片
  37. DeletePhoto(client, id);
  38. Console.WriteLine("Press any key to exit.");
  39. Console.Read();
  40. }
  41. static long UploadPhoto(DefaultAcsClient acsClient, string filepath)
  42. {
  43. // 读取本地文件并计算其MD5值
  44. FileStream stream = new FileStream(filepath, FileMode.Open);
  45. long len = stream.Length;
  46. string md5Str = "";
  47. using (var md5 = MD5.Create())
  48. {
  49. var hash = md5.ComputeHash(stream);
  50. md5Str = BitConverter.ToString(hash).Replace("-", "").ToLowerInvariant();
  51. stream.Close();
  52. }
  53. Console.WriteLine("md5 = " + md5Str);
  54. // 开启上传事务
  55. CreateTransactionRequest request = new CreateTransactionRequest()
  56. {
  57. StoreName = STORE_NAME,
  58. LibraryId = LID_ID,
  59. Md5 = md5Str,
  60. Size = len,
  61. Ext = "jpg",
  62. AcceptFormat = FormatType.JSON
  63. };
  64. // 获取上传照片到OSS的相关信息
  65. CreateTransactionResponse response = acsClient.GetAcsResponse(request);
  66. CreateTransactionResponse.CreateTransaction_Transaction trans = response.Transaction;
  67. var upload = trans.Upload;
  68. // 通过OSS Client上传照片到OSS
  69. var ossClient = new OssClient(upload.OssEndpoint, upload.AccessKeyId, upload.AccessKeySecret, upload.StsToken);
  70. ossClient.PutObject(upload.Bucket, upload.ObjectKey, filepath);
  71. // 提交上传事务,完成文件上传
  72. CreatePhotoRequest createPhotoRequest = new CreatePhotoRequest()
  73. {
  74. StoreName = STORE_NAME,
  75. LibraryId = LID_ID,
  76. FileId = upload.FileId,
  77. SessionId = upload.SessionId,
  78. PhotoTitle = "no title",
  79. UploadType = "manual",
  80. AcceptFormat = FormatType.JSON
  81. };
  82. var createPhotoResponse = acsClient.GetAcsResponse(createPhotoRequest);
  83. long photoId = (long)createPhotoResponse.Photo.Id;
  84. Console.WriteLine("uploaded photo, id = " + photoId);
  85. return photoId;
  86. }
  87. static void GetAccessUrl(DefaultAcsClient acsClient, long photoId)
  88. {
  89. GetThumbnailRequest request = new GetThumbnailRequest()
  90. {
  91. StoreName = STORE_NAME,
  92. LibraryId = LID_ID,
  93. PhotoId = photoId,
  94. ZoomType = "image/resize,m_lfit,h_1280,w_1280",
  95. AcceptFormat = FormatType.JSON
  96. };
  97. var response = acsClient.GetAcsResponse(request);
  98. Console.WriteLine("access url = " + response.ThumbnailUrl);
  99. }
  100. static void DeletePhoto(DefaultAcsClient acsClient, long photoId)
  101. {
  102. List<long?> ids = new List<long?>();
  103. ids.Add(photoId);
  104. DeletePhotosRequest request = new DeletePhotosRequest()
  105. {
  106. StoreName = STORE_NAME,
  107. LibraryId = LID_ID,
  108. PhotoIds = ids,
  109. AcceptFormat = FormatType.JSON
  110. };
  111. acsClient.GetAcsResponse(request);
  112. }
  113. }
  114. }