当前位置:   article > 正文

unity oss上传文件_unity 自动上传oss 代码

unity 自动上传oss 代码

今天研究了下在 unity 下做的 oss 上传,感觉应该分享下。

下面是官网的链接文档
https://help.aliyun.com/document_detail/91093.html?spm=a2c4g.11186623.6.908.1c712fc0rB0SQB
官网给的例子

using Aliyun.OSS;
var endpoint = "<yourEndpoint>";
var accessKeyId = "<yourAccessKeyId>";
var accessKeySecret = "<yourAccessKeySecret>";
var bucketName = "<yourBucketName>";
var objectName = "<yourObjectName>";
var localFilename = "<yourLocalFilename>";
// 创建OssClient实例。
var client = new OssClient(endpoint, accessKeyId, accessKeySecret);
try
{
    // 上传文件。
    client.PutObject(bucketName, objectName, localFilename);
    Console.WriteLine("Put object succeeded");
}
catch (Exception ex)
{
    Console.WriteLine("Put object failed, {0}", ex.Message);
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

<yourAccessKeyId><yourAccessKeySecret><yourLocalFilename>这三个参数相对简单些,分别是你的 Oss 帐号的 Id、secret、本地文件的路径。
而对于 <yourEndpoint><yourBucketName><yourObjectName>却不知所云,他们到底是个什么,其实在 oss 里这个都有。打开你的 oss 后台管理。

在这里插入图片描述

其中 1 代表 <yourBucketName>。2代表<yourEndpoint>,但是前面要加上 http://或者 https://

<yourObjectName>就是你的最终文件的路径, 例如:modal/oxs/aaa.unity3d

下面是我做了 AssetsBundle后,自动上传ab包的功能。
附上写好的Oss单例代码:

using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using Aliyun.OSS;
using UnityEditor;

public class AliyunOss{
	// 创建OssClient实例。
	OssClient client;
	string endpoint = "https://oss-cn-beijing.aliyuncs.com";
	string accessKeyId = "";
	string accessKeySecret = "";
	string bucketName = "abcdef";
	string objectName = "modal";
	string localFilename = "Assets/StreamingAssets";
	string outPath = "Assets/StreamingAssets"; //Application.temporaryCachePath
	#region 单例
	private static AliyunOss mSelf = null;
	public static AliyunOss Instance
	{
		get
		{
			if (mSelf == null)
			{
				mSelf = new AliyunOss();
			}
			return mSelf;
		}
	}

	AliyunOss()
	{
		client = new OssClient(endpoint, accessKeyId, accessKeySecret);

	}

	#endregion

	public void OssAb(string path,string name)
	
	{	
		try
		{
			// 上传文件。
			Debug.Log(path+"-----------"+objectName+path+"/"+name);
			client.PutObject(bucketName, objectName+path+"/"+name, localFilename+path+"/"+name);
			Debug.Log("Oss succeeded");
		}
		catch (Exception e)
		{
			Console.WriteLine(e);
			throw;
		}
	}
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58

其中关键语句client.PutObject(bucketName, objectName+path+"/"+name, localFilename+path+"/"+name);为上传文件,被调用后是这样的:
client.PutObject("abcdef","modal/oxs/aaa.unity3d","Assets/StreamingAssets/oxs/aaa.unity3d");

这样是不是就理解了,如果给你解答疑惑,不要忘记点赞哦。

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号