首先登錄阿里云 免費(fèi)試用–對象存儲OSS --點(diǎn)擊立即試用,可以有三個(gè)月的免費(fèi)試用
創(chuàng)建Buket
新建AccessKey ,新建完成后,會有一個(gè)CSV文件,下載下來,里面有Key ,代碼中需要用到
下載SDK
雙擊打開 sln文件,使用VS打開,右鍵項(xiàng)目–屬性,修改程序集名字,然后點(diǎn)擊生成–生成解決方案,這時(shí) sdk/bin 里面就會有 Aliyun.OSS.dll了 然后把這個(gè)dll拖入到Unity 工程里即可(任意位置都可以),
剩下的就寫代碼了
using Aliyun.OSS;
using Aliyun.OSS.Common;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Threading;
using UnityEngine;
using UnityEngine.UI;
public class AliyunOSSWithProcess : MonoBehaviour
{
// UI 的相關(guān)組件變量
public Image processImage;
// Oss對象,文件路徑,文件名變量
private OssClient ossClient;
string filePath;
string fileName;
// 進(jìn)度的回調(diào)函數(shù),以及線程,進(jìn)度變量
Action<float> PutProcessCallback;
Thread putLocalThread;
float putProcess = 0;
// Start is called before the first frame update
void Start()
{
// new OssClient 對象
ossClient = new OssClient(Config.EndPoint, Config.AccessKeyId, Config.AccessKeySecret);
string path = Application.streamingAssetsPath + "/Test.txt";
// 多線程進(jìn)度上傳函數(shù)
PutObjectWithProcessByThread((process) =>
{
Debug.Log("上傳進(jìn)度為:" + process);
},
path,
Path.GetFileName(path.Trim()));
}
// Update is called once per frame
void Update()
{
// 因?yàn)?UI 只能在主線程中,所以在 Update 中監(jiān)控進(jìn)度給 UI
if (PutProcessCallback != null) {
processImage.fillAmount = putProcess;
if (putProcess >= 1) {
PutProcessCallback = null;
putProcess = 0;
}
}
}
/// <summary>
/// 子線程上傳文件,避免卡頓
/// </summary>
/// <param name="action"></param>
/// <param name="filePath"></param>
/// <param name="fileName"></param>
public void PutObjectWithProcessByThread(Action<float> action, string filePath, string fileName)
{
PutProcessCallback = action;
this.fileName = fileName;
this.filePath = filePath;
putLocalThread = new Thread(PutObjectWithProcess);
putLocalThread.Start();
}
/// <summary>
/// 獲取上傳進(jìn)度
/// </summary>
void PutObjectWithProcess()
{
try
{
using (var fs = File.Open(filePath, FileMode.Open))
{
PutObjectRequest putObjectRequest = new PutObjectRequest(Config.Bucket, fileName, fs);
putObjectRequest.StreamTransferProgress += PutStreamProcess;
ossClient.PutObject(putObjectRequest);
Debug.Log("帶有進(jìn)度本地文件上傳成功");
}
}
catch (OssException e)
{
Debug.Log("帶有進(jìn)度本地文件數(shù)據(jù)上傳錯誤:" + e);
}
catch (Exception e)
{
Debug.Log("帶有進(jìn)度本地文件數(shù)據(jù)上傳錯誤:" + e);
}
finally
{
// 終止進(jìn)程
putLocalThread.Abort();
}
}
/// <summary>
/// 文件上傳流事件
/// </summary>
/// <param name="sender"></param>
/// <param name="args"></param>
void PutStreamProcess(object sender, StreamTransferProgressArgs args)
{
putProcess = (args.TransferredBytes * 100 / args.TotalBytes) / 100.0f;
PutProcessCallback.Invoke(putProcess);
}
}
public class Config
{
public const string AccessKeyId = "在上面提到的CSV文件里";
public const string AccessKeySecret = "在上面提到的CSV文件里";
public const string EndPoint = "oss-cn-beijing.aliyuncs.com";
public const string Bucket = "testbuglog";
}
然后腳本掛到場景里,創(chuàng)建一個(gè)Image, 文件路徑已經(jīng)要帶后綴名,然后運(yùn)行就可以了,文章來源:http://www.zghlxwxcb.cn/news/detail-649893.html
借鑒文章文章來源地址http://www.zghlxwxcb.cn/news/detail-649893.html
到了這里,關(guān)于Unity 上傳文件到阿里云 對象存儲OSS服務(wù)器的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!