Unity 上传文件到阿里云 对象存储OSS服务器

首先登录阿里云 免费试用--对象存储OSS --点击立即试用,可以有三个月的免费试用

创建Buket


新建AccessKey ,新建完成后,会有一个CSV文件,下载下来,里面有Key ,代码中需要用到

下载SDK

双击打开 sln文件,使用VS打开,右键项目--属性,修改程序集名字,然后点击生成--生成解决方案,这时 sdk/bin 里面就会有 Aliyun.OSS.dll了 然后把这个dll拖入到Unity 工程里即可(任意位置都可以),


剩下的就写代码了

csharp 复制代码
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 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);

         string path = Application.streamingAssetsPath + "/Test.txt";

        // 多线程进度上传函数
        PutObjectWithProcessByThread((process) =>
        {
            Debug.Log("上传进度为:" + process);
        },
        path,
        Path.GetFileName(path.Trim()));
    }

    // 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 = "在上面提到的CSV文件里"; 
    public const string AccessKeySecret = "在上面提到的CSV文件里";
    public const string EndPoint = "oss-cn-beijing.aliyuncs.com";
    public const string Bucket = "testbuglog";
 
}

然后脚本挂到场景里,创建一个Image, 文件路径已经要带后缀名,然后运行就可以了,

借鉴文章

相关推荐
北邮刘老师8 小时前
A3C Network:智能体互联网的层次化视图
运维·服务器·网络
XRJ040618xrj8 小时前
如何在Linux中根据物理网卡建立虚拟网卡
linux·服务器·网络
空中楼阁,梦幻泡影9 小时前
LoRA 详细解析,使用LoRA 方式对模型进行微调详细操作指南
运维·服务器·人工智能·机器学习·语言模型
晚风吹长发9 小时前
初步了解Linux中的动静态库及其制作和使用
linux·运维·服务器·数据结构·c++·后端·算法
Le_ee9 小时前
dc4打靶报告
运维·服务器·网络
Dr.勿忘10 小时前
MUMU模拟器adb连接失败:cannot connect to 127.0.0.1:16384: 由于目标计算机积极拒绝,无法连接。 (10061)
游戏·unity·adb·游戏程序·调试·模拟器
4t4run10 小时前
28、Linux 系统定时任务
linux·运维·服务器
cui__OaO10 小时前
Linux驱动--基于驱动设备分离的按键中断驱动
linux·运维·服务器·嵌入式
OnlyEasyCode11 小时前
Linux下载Navicat、特定版本Mysql
linux·运维·服务器
顾北1211 小时前
阿里百炼AI大模型接入指南
阿里云·ai编程