unity oss上傳檔案
阿新 • • 發佈:2018-11-07
今天研究了下在 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");
這樣是不是就理解了,如果給你解答疑惑,不要忘記點贊哦。