Unity 阿里云 之 OSS对象存储功能的接入之数据上传

Unity 阿里云 之 OSS对象存储功能的接入之数据上传

 

目录

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 ,文件里查看上传文件

  • 7
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
Unity是一种跨平台的游戏开发引擎,可以用于开发iOS、Android、Windows等多种平台的游戏。而阿里云则是阿里巴巴集团提供的云计算服务平台,该平台提供了各种云服务器、存储、数据库等服务,方便开发者部署和管理应用程序。 Unity阿里云之间的接口主要包括以下几个方面: 1. 阿里云云服务器(ECS)集成:Unity可以使用阿里云提供的API和SDK进行与ECS实例的交互,包括创建、删除、管理云服务器等。 2. 阿里云对象存储OSS)集成:通过阿里云OSS服务,Unity可以实现游戏素材的存储和管理,包括上传、下载、删除等操作。 3. 阿里云消息服务(MNS)集成:Unity可以通过阿里云的消息服务,实现游戏之间的消息传递和通信,方便实现多人游戏或在线功能。 4. 阿里云数据库服务(RDS)集成:Unity可以使用阿里云提供的数据库服务,如MySQL、SQL Server等,在游戏中存储和管理用户数据、排行榜等信息。 要实现Unity阿里云的接口,首先要根据阿里云提供的开发文档和教程了解相关的API和SDK的使用方法。然后,在Unity项目中导入相应的SDK,并根据文档进行配置和设置。最后,在代码中调用相应的API和方法,实现与阿里云的交互。 总之,Unity阿里云的接口集成可以为游戏开发者提供强大的云计算能力和存储服务,使得游戏开发和部署更加便捷和灵活。通过学习和掌握相关的教程和文档,开发者可以使用Unity开发出更具有丰富性和可扩展性的游戏应用。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

仙魁XAN

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值