下面代码演示如何使用智能云相册的.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);
}
}
}
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。