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);
}

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

在这里插入图片描述

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

<yourBucketName>就是你的最终文件的路径, 例如: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;
		}
	}
}

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

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

猜你喜欢

转载自blog.csdn.net/u014196765/article/details/83651329