阿里云存储封装类 AliYunOssUtility
using Aliyun.OpenServices;using Aliyun.OpenServices.OpenStorageService;using System;using System.Collections.Generic;using System.Configuration;using System.Drawing;using System.IO;using System
·
using Aliyun.OpenServices;
using Aliyun.OpenServices.OpenStorageService;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
namespace Utility
{
public class AliYunOssUtility
{
#region FileTypeEnum
public enum FileTypeEnum
{
none,
jpg,
gif,
png,
mp4,
doc,
docx,
ppt,
pptx,
rar,
zip
}
#endregion
#region private
private String BucketName;
private ClientConfiguration clienConfig;
private Uri uri = new Uri("http://oss.aliyuncs.com");
private readonly string aliyunKey = ConfigurationManager.AppSettings["AppAliYunKey"] != null ? ConfigurationManager.AppSettings["AppAliYunKey"].ToString() : "*********";
private readonly string aliyunSecrect = ConfigurationManager.AppSettings["AppAliYunSecrect"] != null ? ConfigurationManager.AppSettings["AppAliYunSecrect"].ToString() : "*******";
private Aliyun.OpenServices.OpenStorageService.OssClient ossClient;
private Aliyun.OpenServices.OpenStorageService.ObjectMetadata meta;
#endregion
public AliYunOssUtility(string bucketName = "upload")
{
BucketName = bucketName;
Init();
}
public void Init()
{
clienConfig = new ClientConfiguration();
clienConfig.ConnectionTimeout = 300000;
uri = new Uri("http://oss.aliyuncs.com");
ossClient = new Aliyun.OpenServices.OpenStorageService.OssClient(uri, aliyunKey, aliyunSecrect, clienConfig);
meta = new Aliyun.OpenServices.OpenStorageService.ObjectMetadata();
}
#region PutObject
public void PutObject(string key, Stream stream, FileTypeEnum fileType)
{
switch (fileType)
{
case FileTypeEnum.jpg: meta.ContentType = "image/jpeg"; break;
case FileTypeEnum.gif: meta.ContentType = "image/gif"; break;
case FileTypeEnum.png: meta.ContentType = "image/png"; break;
case FileTypeEnum.mp4: meta.ContentType = "video/mp4"; break;
case FileTypeEnum.doc: meta.ContentType = "application/msword"; break;
case FileTypeEnum.ppt: meta.ContentType = "applications-powerpoint"; break;
case FileTypeEnum.rar: meta.ContentType = "application/x-rar-compressed"; break;
case FileTypeEnum.zip: meta.ContentType = "application/x-zip-compressed"; break;
}
PutObjectResult result = ossClient.PutObject(BucketName, key, stream, meta);
}
#endregion
#region PutObject fileName
public void PutObject(string key, string fileName, FileTypeEnum fileType = FileTypeEnum.none)
{
FileStream fileStream = new FileStream(fileName, FileMode.Open);
fileStream.Seek(0, SeekOrigin.Begin);
string FileExtension = fileName.Substring(fileName.LastIndexOf('.')).ToLower();
if (fileType == FileTypeEnum.none)
{
switch (FileExtension)
{
case ".jpg": fileType = FileTypeEnum.jpg; break;
case ".gif": fileType = FileTypeEnum.gif; break;
case ".png": fileType = FileTypeEnum.png; break;
case ".mp4": fileType = FileTypeEnum.mp4; break;
case ".doc":
case ".docx": fileType = FileTypeEnum.doc; break;
case ".ppt":
case ".pptx": fileType = FileTypeEnum.ppt; break;
case ".rar": fileType = FileTypeEnum.rar; break;
case ".zip": fileType = FileTypeEnum.zip; break;
}
}
PutObject(key, fileStream, fileType);
fileStream.Close();
}
#endregion
#region GetBitmap
public Bitmap GetBitmap(string key)
{
OssObject ossObject = null;
Bitmap bmp = null;
try
{
ossObject = ossClient.GetObject(BucketName, key);
if (ossObject != null && ossObject.Content != null)
{
bmp = new Bitmap((Image)new Bitmap(ossObject.Content));
//string imgurl = ossClient.GeneratePresignedUri(bucketName, key, DateTime.Now.AddMinutes(10)).AbsoluteUri;
}
}
catch
{
}
return bmp;
}
#endregion
public void Delete(string key)
{
ossClient.DeleteObject(BucketName, key);
}
}
}
更多推荐
已为社区贡献4条内容
所有评论(0)