public static class OssExpand {
private static OssClient ossClient= new OssClient(Config.EndPoint, Config.AccessKeyId, Config.AccessKeySecret); //
// 上傳
public static string PutObjWithTexture(string localPath, string fileName)
{
// localPath 本地檔案的地址,帶檔案格式 fileName 檔案名 自定義
string ossPath = "";
try
{
string nowYear = System.DateTime.Now.Year.ToString();
string nowMonth = System.DateTime.Now.Month.ToString();
string nowDay = System.DateTime.Now.Day.ToString();
ossPath = nowYear + "/" + nowMonth + "/" + nowDay + "/" + fileName;
ossClient.PutObject(Config.Bucke, ossPath, localPath); //上傳的方法 1:存盤桶 2: 存盤到oss的位置 3: 本地檔案的位置
Debug.Log("上傳成功" + ossPath);
}
catch (OssException e)
{
Debug.Log("本地上傳報錯:" + e.Message);
}
catch (System.Exception e)
{
Debug.Log("本地上傳報錯:" + e.Message);
}
return ossPath;
}
// 下載
public static Texture2D GetObjWithTexture(string objectName) //objectName 指定路徑的檔案包含檔案型別
{
// 下載檔案到流。OssObject 包含了檔案的各種資訊,如檔案所在的存盤空間、檔案名、元資訊以及一個輸入流。
Texture2D texture = new Texture2D(1080, 1080);
try
{
var obj = ossClient.GetObject(Config.Bucke, objectName.Split('.')[0]);
using (var requestStream = obj.Content)
{
byte[] buffer = new byte[requestStream.Length + 1024];
byte[] buf = new byte[1024];
var len = 0;
int index = 0;
// 通過輸入流將檔案的內容讀取到檔案或者記憶體中。
while ((len = requestStream.Read(buf, 0, 1024)) != 0)
{
Array.Copy(buf, 0, buffer, index, buf.Length);
index += len;
}
texture.LoadImage(buffer);
texture.Apply();
Debug.Log("buffer.Length:" + buffer.Length);
}
}
catch (IOException e)
{
Debug.Log("oss下載報錯:" + e.Message);
}
return texture;
}
}
public class Config
{
public const string AccessKeyId = "";
public const string AccessKeySecret = "";
public const string EndPoint = "";
public const string Bucke = "";
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/14030.html
標籤:Unity3D
