开发者社区> 问答> 正文

智能云相册.Net SDK如何使用?

智能云相册.Net SDK如何使用?

展开
收起
小天使爱美 2020-03-31 13:53:38 821 0
1 条回答
写回答
取消 提交回答
  • 下面代码演示如何使用智能云相册的.Net SDK来完成照片上传、获取照片缩略图和删除照片等基本功能。请参考代码中的注释填写相关配置。

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Aliyun.Acs.Core; using Aliyun.Acs.Core.Profile; using Aliyun.Acs.CloudPhoto.Model.V20170711; using Aliyun.Acs.Core.Http; using System.IO; using System.Security.Cryptography; using Aliyun.OSS; namespace ConsoleApp1 { class Program { // 具有访问智能云相册权限的AK,可以是主账号的AK,也可以是授权后的子账号AK。 static string ACCESS_KEY_ID = ""; static string ACCESS_KEY_SECRET = ""; // 修改为您的StoreName和照片库Id static string STORE_NAME = ""; static string LID_ID = ""; // 本地待上传文件的路径 static string FILE_TO_UPLOAD = ""; static void Main(string[] args) { // 构建Client对象 IClientProfile profile = DefaultProfile.GetProfile("cn-shanghai", ACCESS_KEY_ID, ACCESS_KEY_SECRET); DefaultAcsClient client = new DefaultAcsClient(profile); // 上传照片 long id = UploadPhoto(client, FILE_TO_UPLOAD); // 获取照片的访问地址 GetAccessUrl(client, id); Console.WriteLine("Press any key to continue."); Console.Read(); // 删除照片 DeletePhoto(client, id); Console.WriteLine("Press any key to exit."); Console.Read(); } static long UploadPhoto(DefaultAcsClient acsClient, string filepath) { // 读取本地文件并计算其MD5值 FileStream stream = new FileStream(filepath, FileMode.Open); long len = stream.Length; string md5Str = ""; using (var md5 = MD5.Create()) { var hash = md5.ComputeHash(stream); md5Str = BitConverter.ToString(hash).Replace("-", "").ToLowerInvariant(); stream.Close(); } Console.WriteLine("md5 = " + md5Str); // 开启上传事务 CreateTransactionRequest request = new CreateTransactionRequest() { StoreName = STORE_NAME, LibraryId = LID_ID, Md5 = md5Str, Size = len, Ext = "jpg", AcceptFormat = FormatType.JSON }; // 获取上传照片到OSS的相关信息 CreateTransactionResponse response = acsClient.GetAcsResponse(request); CreateTransactionResponse.CreateTransaction_Transaction trans = response.Transaction; var upload = trans.Upload; // 通过OSS Client上传照片到OSS var ossClient = new OssClient(upload.OssEndpoint, upload.AccessKeyId, upload.AccessKeySecret, upload.StsToken); ossClient.PutObject(upload.Bucket, upload.ObjectKey, filepath); // 提交上传事务,完成文件上传 CreatePhotoRequest createPhotoRequest = new CreatePhotoRequest() { StoreName = STORE_NAME, LibraryId = LID_ID, FileId = upload.FileId, SessionId = upload.SessionId, PhotoTitle = "no title", UploadType = "manual", AcceptFormat = FormatType.JSON }; var createPhotoResponse = acsClient.GetAcsResponse(createPhotoRequest); long photoId = (long)createPhotoResponse.Photo.Id; Console.WriteLine("uploaded photo, id = " + photoId); return photoId; } static void GetAccessUrl(DefaultAcsClient acsClient, long photoId) { GetThumbnailRequest request = new GetThumbnailRequest() { StoreName = STORE_NAME, LibraryId = LID_ID, PhotoId = photoId, ZoomType = "image/resize,m_lfit,h_1280,w_1280", AcceptFormat = FormatType.JSON }; var response = acsClient.GetAcsResponse(request); Console.WriteLine("access url = " + response.ThumbnailUrl); } static void DeletePhoto(DefaultAcsClient acsClient, long photoId) { List<long?> ids = new List<long?>(); ids.Add(photoId); DeletePhotosRequest request = new DeletePhotosRequest() { StoreName = STORE_NAME, LibraryId = LID_ID, PhotoIds = ids, AcceptFormat = FormatType.JSON }; acsClient.GetAcsResponse(request); } } }

    2020-03-31 13:53:48
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
从 SDK 到编解码:视频直播架构解析 立即下载
跨平台的云服务SDK需要什么 立即下载
一个跨平台的云服务SDK需要什么 立即下载