Unity 阿里云 之 OSS对象存储功能的接入之数据上传
目录
一、简介和目的
海量、安全、低成本、高可靠的云存储服务,提供99.9999999999%的数据可靠性。使用RESTful API 可以在互联网任何位置存储和访问,容量和处理能力弹性扩展,多种存储类型供选择全面优化存储成本。
在Unity可用于文件的上传下载,只要引入对应的 dll 文件即可使用,很是方便。
二、实现要点
1、在阿里云下载SDK,得到里面的 Aliyun.OSS.dll,引入Unity
2、在OSS对象存储 中新建一个公有的 Bucket
3、申请阿里云 AccessKey
4、关键代码
ossClient = new OssClient(...);
ossClient.PutObject(...);
public class Config
{
public const string AccessKeyId = "<accessKeyId>";
public const string AccessKeySecret = "<accessSecret>";
public const string EndPoint = "oss-cn-shenzhen.aliyuncs.com";
public const string Bucket = "aliyunoss1116";
}
三、使用注意
1、注意:在上传大文件的时候,请使用线程,避免主线程卡顿
2、注意:在使用多线程的时候,注意 UI 相关必须在 主线程中,不然会报错
四、测试效果
五、实现步骤
1、登陆阿里云平台,找到 对象存储 OSS
2、点击进入后,进入 管理控制台
3、创建 Bucket ,根据你的地区选择就近服务器即可
(注意:Bucket 需要读写,请选择 读写权限为 公共读写)
4、下载SDK,解压SDK包,然后 把 Aliyun.OSS.dll 引入Unity
5、对了,如果没有 AccessKey ,记得申请一个
6、获得后面 OssClient 的用到的 EndPoint 和 Bucket (根据自己新建的 Bucket 对号入座即可)
6、上传字符串数据到新建的 Bucket ,代码如下
using System.IO;
using System.Text;
using System.Threading;
using UnityEngine;
public class AliyunOSS : MonoBehaviour
{
private OssClient ossClient;
// Start is called before the first frame update
void Start()
{
ossClient = new OssClient(Config.EndPoint,Config.AccessKeyId,Config.AccessKeySecret);
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.Space)) {
PutObjWithString("test1116.txt","Test AliYun Oss PutObjectString");
}
}
public void PutObjWithString(string fileName,string fileContent) {
try {
byte[] b = Encoding.UTF8.GetBytes(fileContent);
using (Stream stream = new MemoryStream(b)) {
ossClient.PutObject(Config.Bucket,fileName, stream);
Debug.Log("字符串上传成功");
}
}
catch (OssException e) {
Debug.Log("字符串数据上传错误:"+ e);
}
catch (Exception e)
{
Debug.Log("字符串数据上传错误:" + e);
}
}
}
public class Config
{
public const string AccessKeyId = "<accessKeyId>";
public const string AccessKeySecret = "<accessSecret>";
public const string EndPoint = "oss-cn-shenzhen.aliyuncs.com";
public const string Bucket = "aliyunoss1116";
}
7、下面为了避免上传大文件,主线程卡顿,这里使用多线程,进行上传文件,首先Unity ,UI 布局
8、多线程进度上传文件代码及说明如下
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 的相关组件变量
public InputField filePathInputField;
public Button putButton;
public Image processImage;
// Oss对象,文件路径,文件名变量
private OssClient ossClient;
string filePath;
string fileName;
// 进度的回调函数,以及线程,进度变量
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);
// 绑定按钮事件
putButton.onClick.AddListener(()=> {
if (filePathInputField == null && filePathInputField.text == "") {
return;
}
string path = filePathInputField.text.Trim();
// 多线程进度上传函数
PutObjectWithProcessByThread((process) => {
Debug.Log("上传进度为:" + process);
},
filePathInputField.text,
Path.GetFileName(path));
});
}
// Update is called once per frame
void Update()
{
// 因为 UI 只能在主线程中,所以在 Update 中监控进度给 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>
/// 获取上传进度
/// </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("带有进度本地文件上传成功");
}
}
catch (OssException e)
{
Debug.Log("带有进度本地文件数据上传错误:" + e);
}
catch (Exception e)
{
Debug.Log("带有进度本地文件数据上传错误:" + e);
}
finally
{
// 终止进程
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 = "<accessKeyId>";
public const string AccessKeySecret = "<accessSecret>";
public const string EndPoint = "oss-cn-shenzhen.aliyuncs.com";
public const string Bucket = "aliyunoss1116";
}
9、运行结果
10、可以子啊OSS 自己建的 Bucket ,文件里查看上传文件