Home > other >  Unity ali cloud OSS upload pictures and download
Unity ali cloud OSS upload pictures and download

Time:09-27

Public static class OssExpand {
Private static OssClient OssClient=new OssClient (Config. The EndPoint, the Config. AccessKeyId Config. AccessKeySecret);//

//upload
Public static string PutObjWithTexture (string localPath, string fileName)
{
//localPath the address of a local file, the file format fileName file name custom
String ossPath="";
Try
{
String nowYear=System. A DateTime. Now. Year. The ToString ();
String nowMonth=System. A DateTime. Now. The Month. The ToString ();
String nowDay=System. A DateTime. Now. Day. The ToString ();
OssPath=nowMonth nowYear + "/" + + "/" + nowDay + "/" + fileName;
OssClient. PutObject (Config. Bucke ossPath, localPath);//upload method 1:2: storage barrel storage to the oss position 3: the location of the local file
The Debug Log (" uploaded successfully "+ ossPath);
}
The catch (OssException e)
{
The Debug Log (" local upload error: "+ e.M essage);
}
The catch (System. Exception e)
{
The Debug Log (" local upload error: "+ e.M essage);
}
Return ossPath;
}

//download

Public static Texture2D GetObjWithTexture (string objectName)//objectName file contains file type specified path
{
//download files to the stream, OssObject contains a file of all kinds of information, such as file storage, file name, meta information and an input stream,
Texture2D texture=new Texture2D (1080, 1080);
Try
{
Var obj=ossClient. GetObject (Config. Bucke, objectName. Split ('. ') [0]).
Using (var requestStream=obj. Content)
{
Byte [] buffer=new byte [requestStream Length + 1024].
Byte [] buf=new byte [1024].
Var len=0;
Int index=0;
//by the input stream to read the contents of a file to a file or memory,
While ((len=requestStream. Read (buf, 0, 1024)).=0)
{
Array. Copy (buf, 0, buffer, index, buf. Length);
Index +=len;
}
Texture. LoadImage (buffer);
Texture. The Apply ();
The Debug Log (" buffer. Length: "+ buffer. Length);
}
}
The catch (IOException e)
{
The Debug Log (" oss download error: "+ e.M essage);
}
Return texture;
}
}

Public class Config
{
Public const string AccessKeyId="";
Public const string AccessKeySecret="";
Public const string EndPoint="";
Public const string Bucke="";
}