目錄
一.目的
1.想知道:如何:實作字串上傳
二.參考
1.阿里云SMS&OSS、騰訊云COS接入Unity(Unity2018.1.0)
1.上傳檔案
三.注意
1.Unity切換.net庫版本:切換到是4.x,否則上傳檔案失敗!
四.操作:1:成功
1.創建AccessKey
1.1 獲取AccessKeyId+AccessKeySecret+EndPoint+Bucket
2.代碼:MyALiYunConfig
3.代碼:MyALiYunTest
4.代碼 : MyPutObject
5.運行效果:報錯:Unity上傳字串到阿里云oss報錯“沒有設定實體”
1.1解決報錯:完成:切換.net庫版本:切換到是4.x,之前是2.X版本
6.運行效果:成功:能上傳Txt文本到阿里云服務器
一.目的
1.想知道:如何:實作字串(非本地+臨時的)
二.參考
1.阿里云SMS&OSS、騰訊云COS接入Unity(Unity2018.1.0)
http://www.sikiedu.com/course/303
- good:我參考這個視頻學習的,
1.上傳檔案
https://help.aliyun.com/document_detail/31886.html?spm=a2c4g.11186623.0.0.7b7b3e13dYYRMB
- good:官網教程,值得參考
三.注意
1.Unity切換.net庫版本:切換到是4.x,否則上傳檔案失敗!
四.操作:1:成功
1.創建AccessKey

1.1 獲取AccessKeyId+AccessKeySecret+EndPoint+Bucket
AccessKeyId+AccessKeySecret

EndPoint+Bucket

2.代碼:MyALiYunConfig
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// [Author:xzy][Time:20210904]
/// [Function:Ali cloud server inside some configuration; 功能:阿里云服務器里面的一些配置]
/// </summary>
public class MyALiYunConfig
{
/// <summary>字串:阿里云的KeyID</summary>
public const string AccessKeyId = "去阿里云官網查";
/// <summary>字串:阿里云的KeySecret</summary>
public const string AccessKeySecret = "去阿里云官網查";
/// <summary>字串:阿里云的 EndPoint</summary>
public const string EndPoint = "去阿里云官網查";
/// <summary>字串:阿里云的 Bucket名字</summary>
public const string Bucket = "去阿里云官網查";
}


3.代碼:MyALiYunTest
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// [Author:xzy][Time:20210904]
/// [Function:Ali Cloud function test; 功能: 阿里云功能測驗]
/// </summary>
public class MyALiYunTest : MonoBehaviour
{
// Use this for initialization
void Start()
{
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyUp(KeyCode.A))
{
MyPutObject.Instance.PutObjWithStr("test.txt", "各位帥哥你們好,我是xzy!");
// GameObject.FindWithTag("ScriptsHold").GetComponent<MyPutObject>().PutObjWithStr("test.txt", "各位帥哥你們好,我是xzy!");
Debug.Log("抬起A");
}
}
}

4.代碼 : MyPutObject
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Aliyun.OSS;
using System.Text;
using System.IO;
using Aliyun.OSS.Common;
/// <summary>
/// [Author:xzy][Time:20210904]
/// [Function:Upload files to Ali Cloud server; 功能:上傳檔案到阿里云服務器]
/// </summary>
public class MyPutObject : MonoBehaviour
{
/// <summary>單例:MyPutObject</summary>
public static MyPutObject Instance;
/// <summary>OssClient:oss客戶端</summary>
private OssClient client;
private void Awake()
{
Instance = this;//單例賦值
client = new OssClient(MyALiYunConfig.EndPoint, MyALiYunConfig.AccessKeyId, MyALiYunConfig.AccessKeySecret);//創建客戶端物件
}
/// <summary>
/// [Author:xzy][Time:20210904]
/// [Function:Upload string to Ali cloud server; 功能:上傳 字串 到阿里云服務器]
/// </summary>
/// <param name="_strFileName"></param>
/// <param name="_strText"></param>
public void PutObjWithStr(string _strFileName, string _strText)
{
try
{
byte[] b = Encoding.UTF8.GetBytes(_strText);
//使用using:讓其結束后自動洗掉
using (Stream stream = new MemoryStream(b))
{
client.PutObject(MyALiYunConfig.Bucket, _strFileName, stream);//會一直執行,指導將所有資料上傳成功
Debug.Log("字串上傳成功:" + _strText);
}
}
catch (OssException e)
{
Debug.LogError("字串上傳錯誤:" + e);
}
catch (System.Exception e)
{
Debug.LogError("字串上傳錯誤:" + e);
}
}
字串上傳
//public void PutObjWithStr(string fileName, string text)
//{
// try
// {
// byte[] b = Encoding.UTF8.GetBytes(text);
// using (Stream stream = new MemoryStream(b))
// {
// client.PutObject(MyALiYunConfig.Bucket, fileName, stream);
// Debug.Log("字串上傳成功:" + text);
// }
// }
// catch (OssException e)
// {
// Debug.Log("字串上傳錯誤:" + e);
// }
// catch (System.Exception e)
// {
// Debug.Log("字串上傳錯誤:" + e.Message);
// }
//}
}


5.運行效果:報錯:Unity上傳字串到阿里云oss報錯“沒有設定實體”




1.1解決報錯:完成:切換.net庫版本:切換到是4.x,之前是2.X版本




6.運行效果:成功:能上傳Txt文本到阿里云服務器


轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/297928.html
標籤:其他
上一篇:python打包神器Pyinstaller實用指南(看這一篇就夠了!)
下一篇:unity學習資源
