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

首先登录阿里云 免费试用–对象存储OSS --点击立即试用,可以有三个月的免费试用
在这里插入图片描述
创建Buket
在这里插入图片描述
在这里插入图片描述
新建AccessKey ,新建完成后,会有一个CSV文件,下载下来,里面有Key ,代码中需要用到
在这里插入图片描述
下载SDK
在这里插入图片描述
双击打开 sln文件,使用VS打开,右键项目–属性,修改程序集名字,然后点击生成–生成解决方案,这时 sdk/bin 里面就会有 Aliyun.OSS.dll了 然后把这个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 的相关组件变量

    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, 文件路径已经要带后缀名,然后运行就可以了,
在这里插入图片描述

借鉴文章

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在Unity中使用Aliyun OSS服务器上的视频,你需要先在Unity中安装Aliyun OSS SDK。然后,你需要编写代码来连接到OSS服务器,并下载视频文件。以下是几个简单的步骤: 1. 在Unity中导入OSS SDK。可以从OSS的官方网站https://www.alibabacloud.com/help/zh/doc-detail/32068.htm中下载。 2. 在代码中定义Aliyun OSS的Access Key和Secret Key,以及Bucket名称和Object Key。这些信息可以在OSS的控制台中找到。 3. 使用OSS SDK提供的方法连接到OSS服务器,并下载视频文件。可以使用以下代码: ```csharp using Aliyun.OSS; using UnityEngine; using System.IO; public class DownloadVideo : MonoBehaviour { private OssClient client; void Start() { string accessKeyId = "your-access-key-id"; string accessKeySecret = "your-access-key-secret"; string endpoint = "your-endpoint"; string bucketName = "your-bucket-name"; string objectKey = "your-object-key"; var credentials = new DefaultCredentials(accessKeyId, accessKeySecret); client = new OssClient(endpoint, credentials); var request = new GetObjectRequest(bucketName, objectKey); var result = client.GetObject(request); using (var stream = result.Content) { byte[] buffer = new byte[stream.Length]; stream.Read(buffer, 0, buffer.Length); string path = Application.persistentDataPath + "/video.mp4"; File.WriteAllBytes(path, buffer); } } } ``` 以上代码下载视频文件,并将其保存到应用程序的持久数据路径中。 4. 在Unity中使用VideoPlayer组件播放已下载的视频文件。可以使用以下代码: ```csharp using UnityEngine; using UnityEngine.Video; public class PlayVideo : MonoBehaviour { public string videoPath = ""; void Start() { var videoPlayer = GetComponent<VideoPlayer>(); videoPlayer.url = videoPath; videoPlayer.Play(); } } ``` 以上代码将视频文件的路径设置为VideoPlayer组件的url属性,并开始播放视频。 请注意,以上代码只是一个简单的示例,你需要根据自己的需求进行修改。例如,你可能需要添加错误处理、进度更新等功能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值