You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

99 lines
4.1 KiB
C#

11 months ago
using System;
using System.IO;
using Aliyun.OSS;
namespace BP.Tools
{
public static class OSSUtil
{
/// <summary>
/// <20><><EFBFBD><EFBFBD>OSS<53><53><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD><EFBFBD>
/// </summary>
/// <exception cref="Exception"></exception>
private static void CheckOSSConfig()
{
if (BP.DA.DataType.IsNullOrEmpty(BP.Difference.SystemConfig.OSSEndpoint))
throw new Exception("err@<40><><EFBFBD>⵽û<E2B5BD><C3BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>OSSEndpoint<6E><74><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>OSSEndpoint<6E><74><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ϴ<EFBFBD><CFB4>ļ<EFBFBD>");
if (BP.DA.DataType.IsNullOrEmpty(BP.Difference.SystemConfig.OSSAccessKeyId))
throw new Exception("err@<40><><EFBFBD>⵽û<E2B5BD><C3BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>OSSAccessKeyId<49><64><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>OSSAccessKeyId<49><64><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ϴ<EFBFBD><CFB4>ļ<EFBFBD>");
if (BP.DA.DataType.IsNullOrEmpty(BP.Difference.SystemConfig.OSSAccessKeySecret))
throw new Exception("err@<40><><EFBFBD>⵽û<E2B5BD><C3BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>OSSAccessKeySecret<65><74><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>OSSAccessKeySecret<65><74><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ϴ<EFBFBD><CFB4>ļ<EFBFBD>");
if (BP.DA.DataType.IsNullOrEmpty(BP.Difference.SystemConfig.OSSBucketName))
throw new Exception("err@<40><><EFBFBD>⵽û<E2B5BD><C3BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>OSSBucketName<6D><65><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>OSSBucketName<6D><65><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ϴ<EFBFBD><CFB4>ļ<EFBFBD>");
}
/// <summary>
/// OSS<53>ϴ<EFBFBD><CFB4>ļ<EFBFBD>
/// </summary>
/// <param name="objectName">Object<63><74><EFBFBD><EFBFBD>·<EFBFBD><C2B7></param>
/// <param name="temp"><3E><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD>ļ<EFBFBD>·<EFBFBD><C2B7></param>
/// <exception cref="Exception"></exception>
public static void doUpload(string objectName, string temp)
{
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
CheckOSSConfig();
// <20><><EFBFBD><EFBFBD>OssClientʵ<74><CAB5><EFBFBD><EFBFBD>
var client = new OssClient(BP.Difference.SystemConfig.OSSEndpoint,
BP.Difference.SystemConfig.OSSAccessKeyId, BP.Difference.SystemConfig.OSSAccessKeySecret);
if (objectName.StartsWith("/"))
objectName = objectName.Substring(1);
try
{
// <20>ϴ<EFBFBD><CFB4>ļ<EFBFBD><C4BC><EFBFBD>
client.PutObject(BP.Difference.SystemConfig.OSSBucketName, objectName, temp);
}
catch (Exception ex)
{
throw new Exception("err@ <20>ϴ<EFBFBD>ʧ<EFBFBD><CAA7>, <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʾ<EFBFBD><CABE>Ϣ<EFBFBD><CFA2>" + ex.Message);
}
}
/// <summary>
/// OSS<53><53><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD>
/// </summary>
/// <param name="fileFullNamem"><3E>ļ<EFBFBD>ȫ·<C8AB><C2B7><EFBFBD><EFBFBD></param>
/// <param name="tempFile"><3E><>ʱ<EFBFBD>ļ<EFBFBD>·<EFBFBD><C2B7></param>
/// <exception cref="Exception"></exception>
public static void doDownload(string fileFullNamem, string tempFile)
{
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
CheckOSSConfig();
// <20><><EFBFBD><EFBFBD>OssClientʵ<74><CAB5><EFBFBD><EFBFBD>
var client = new OssClient(BP.Difference.SystemConfig.OSSEndpoint,
BP.Difference.SystemConfig.OSSAccessKeyId, BP.Difference.SystemConfig.OSSAccessKeySecret);
try
{
string subDir = "";
//<2F>ж<EFBFBD><D0B6>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD><EFBFBD>Ŀ¼·<C2BC><C2B7>
if (BP.DA.DataType.IsNullOrEmpty(BP.Difference.SystemConfig.BucketSubPath) == false && "/".Equals(BP.Difference.SystemConfig.BucketSubPath) == false)
{
subDir = BP.Difference.SystemConfig.BucketSubPath + "/";
}
//ת<><D7AA>·<EFBFBD><C2B7>
fileFullNamem = subDir + fileFullNamem.Replace("//", "/");
if (fileFullNamem.StartsWith("/"))
fileFullNamem = fileFullNamem.Substring(1);
// <20><><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>OssObject<63><74><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD><C4BC>ĸ<EFBFBD><C4B8><EFBFBD><EFBFBD><EFBFBD>Ϣ<EFBFBD><CFA2><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD>ڵĴ洢<C4B4>ռ<D5BC>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD><EFBFBD>Ԫ<EFBFBD><D4AA>Ϣ<EFBFBD>Լ<EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
var obj = client.GetObject(BP.Difference.SystemConfig.OSSBucketName, fileFullNamem);
using (var requestStream = obj.Content)
{
byte[] buf = new byte[1024];
var fs = File.Open(tempFile, FileMode.OpenOrCreate);
var len = 0;
// ͨ<><CDA8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݶ<EFBFBD>ȡ<EFBFBD><C8A1><EFBFBD>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڴ<EFBFBD><DAB4>С<EFBFBD>
while ((len = requestStream.Read(buf, 0, 1024)) != 0)
{
fs.Write(buf, 0, len);
}
fs.Close();
}
}
catch (Exception ex)
{
throw new Exception("err@<40><><EFBFBD><EFBFBD>ʧ<EFBFBD><CAA7>.<2E><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʾ<EFBFBD><CABE>Ϣ<EFBFBD><CFA2> " + ex.Message);
}
}
}
}