How do I upload files from a phone to my Amazon S3 server? - unity3d

I'm developing a mobile app with Unity and using S3 to store and retrieve assets, I can download asset bundles just fine from the server to the phone, but how do I upload files from the phone to the server?
I used the PostObject function from the AWS Unity SDK, and it works fine if I upload from the computer as I know the directory, but I'm not sure how to get the phone's photo gallery to upload to the s3 server.
This is the PostObject function
public void PostObject(string fileName)
{
ResultText.text = "Retrieving the file";
var stream = new FileStream("file://" + Application.streamingAssetsPath + "/" + fileName,
FileMode.Open, FileAccess.Read, FileShare.Read);
Debug.Log("kek");
ResultText.text += "\nCreating request object";
var request = new PostObjectRequest()
{
Bucket = S3BucketName,
Key = fileName,
InputStream = stream,
CannedACL = S3CannedACL.Private,
Region = _S3Region
};
ResultText.text += "\nMaking HTTP post call";
Client.PostObjectAsync(request, (responseObj) =>
{
if (responseObj.Exception == null)
{
ResultText.text += string.Format("\nobject {0} posted to bucket {1}",
responseObj.Request.Key, responseObj.Request.Bucket);
}
else
{
ResultText.text += "\nException while posting the result object";
ResultText.text += string.Format("\n receieved error {0}",
responseObj.Response.HttpStatusCode.ToString());
}
});
}
And this is where I'm using it to upload the picture taken from the phone to the server
public void TakePicture(int maxSize)
{
NativeCamera.Permission permission = NativeCamera.TakePicture((path) =>
{
Debug.Log("Image path: " + path);
if (path != null)
{
// Create a Texture2D from the captured image
Texture2D imageTexture = NativeCamera.LoadImageAtPath(path, maxSize);
if (imageTexture == null)
{
Debug.Log("Couldn't load texture from " + path);
return;
}
//picturePreview.gameObject.SetActive(true);
//picturePreview.texture = imageTexture;
Texture2D readableTexture = DuplicateTexture(imageTexture);
StartCoroutine(AddImageJob(readableTexture));
//Saves taken photo to the Image Gallery
if (isSaveFiles)
{
NativeGallery.SaveImageToGallery(imageTexture, "AReview", "test");
//Upload to Amazon S3
aws.PostObject(imageTexture.name);
aws.PostObject("test");
}
}
}, maxSize);
Debug.Log("Permission result: " + permission);
}
Any clues?
Thank you.

Related

Save and Load file in HoloLens using Unity editer

I'm trying to Save and Load Vector3 Coordinates in HoloLens App the program works on my laptop using unity but it will not save or load a file in the HoloLens.
Here is the program I am using to create the path for saving and loading. Any help would be appreciated.
public GameData Load()
{
string fullPath = Path.Combine(dataDirPath, dataFileName);
GameData loadedData = null;
if (File.Exists(fullPath))
{
try
{
string dataToLoad = "";
using (FileStream stream = new FileStream(fullPath, FileMode.Open))
{
using (StreamReader reader = new StreamReader(stream))
{
dataToLoad = reader.ReadToEnd();
}
}
loadedData = JsonUtility.FromJson<GameData>(dataToLoad);
}
catch (Exception e)
{
UnityEngine.Debug.LogError("Error occured when trying to load data from filr: " + fullPath + "\n" + e);
}
}
return loadedData;
}
public void Save (GameData data)
{
string fullPath = Path.Combine(dataDirPath, dataFileName);
try
{
Directory.CreateDirectory(Path.GetDirectoryName(fullPath));
string dataToStore = JsonUtility.ToJson(data, true);
using (FileStream stream = new FileStream(fullPath, FileMode.Create))
{
using (StreamWriter writer = new StreamWriter(stream))
{
writer.Write(dataToStore);
}
}
}
catch (Exception e)
{
UnityEngine.Debug.LogError("Error occured when trying to save data to file: " + fullPath + "\n" + e);
}
}
}
If I understand this correctly, you're debugging remotely. To clarify, your application cannot access files on HoloLens until you deploy it to HoloLens. And for how to access files on HoloLens, you can refer Create, write, and read a file - UWP applications | Microsoft Learn and Working with File on Hololens 2 (UWP to .NET).

Downloading images and save to device

I having some issues here with UnityWebRequest.
I tried to download and save the jpeg, but it seem that the download is a success but it does not save it, and does not show me Log from "saveToFile" function.
Did I did something wrong?
Here are my code.
public string folderPath;
void Start()
{
folderPath = Application.persistentDataPath + "/" + FileFolderName;
}
IEnumerator DownloadingImage(Uri url2)
{
Debug.Log("Start Downloading Images");
using (UnityWebRequest uwr = UnityWebRequestTexture.GetTexture(url2))
{
// uwr2.downloadHandler = new DownloadHandlerBuffer();
yield return uwr.SendWebRequest();
if (uwr.isNetworkError || uwr.isHttpError)
{
Debug.Log(uwr.error);
}
else
{
Debug.Log("Success");
Texture myTexture = DownloadHandlerTexture.GetContent(uwr);
byte[] results = uwr.downloadHandler.data;
saveImage(folderPath, results);
}
}
}
void saveImage(string path, byte[] imageBytes)
{
//Create Directory if it does not exist
if (!Directory.Exists(Path.GetDirectoryName(path)))
{
Directory.CreateDirectory(Path.GetDirectoryName(path));
Debug.Log("Creating now");
}
else
{
Debug.Log(path + " does exist");
}
try
{
File.WriteAllBytes(path, imageBytes);
Debug.Log("Saved Data to: " + path.Replace("/", "\\"));
}
catch (Exception e)
{
Debug.LogWarning("Failed To Save Data to: " + path.Replace("/", "\\"));
Debug.LogWarning("Error: " + e.Message);
}
}
You have wrong file name so give filename with extension,
If you don't give extension, 'Directory.Exists' doesn't know whether file or directory.
or you could separate parameters such as rootDirPath and filename.
IEnumerator DownloadingImage(Uri url2)
{
Debug.Log("Start Downloading Images");
using (UnityWebRequest uwr = UnityWebRequestTexture.GetTexture(url2))
{
// uwr2.downloadHandler = new DownloadHandlerBuffer();
yield return uwr.SendWebRequest();
if (uwr.isNetworkError || uwr.isHttpError)
{
Debug.Log(uwr.error);
}
else
{
Debug.Log("Success");
Texture myTexture = DownloadHandlerTexture.GetContent(uwr);
byte[] results = uwr.downloadHandler.data;
string filename = gameObject.name+".dat";
// saveImage(folderPath, results); // Not a folder path
saveImage(folderPath+"/"+filename, results); // give filename
}
}
}

How to attach multiple videos and photos to a status post with Facebook graph API

I'm trying to publish a post with multiple attached videos and photos via the Facebook Graph API. I uploaded the videos and photos first using a batch request and get the ids successfully for the uploaded media. Then I pass the media ids along with the post data using attached_media. Things work fine for single or multiple photos. But not for a single video or multiple videos. I got this error: "Graph returned an error: (#10) Application does not have permission for this action" whenever ids of videos are included in attached_media.
I know as a user you have the ability to attach multiple videos and photos to a Facebook post. Does the Facebook Graph API just flat out not support this for multiple videos?
I am working with C# and Unity
Here is the function that uploads videos and then sends a post with attached result videos ids:
private IEnumerator UploadMediaWithStatus(string message, string[] mediaUrls)
{
FacebookPostStatusUpdatedEvent.Invoke("Submitting FB post ... ");
var curPage = m_UserPages[FacebookFeedsList.GetInstance().ActiveFeed] as IDictionary<string, object>;
var fbClient = new FacebookClient(curPage["access_token"].ToString());
List<string> mediaIDs = new List<string> ();
foreach (string mediaUrl in mediaUrls)
{
WWW _media = new WWW("file://" + mediaUrl);
yield return _media;
if (!System.String.IsNullOrEmpty(_media.error))
{
FacebookPostStatusUpdatedEvent.Invoke("FB post failed: error loading media " + mediaUrl);
//TruLogger.LogError("cant load Image : " + _image.error);
yield break;
}
byte[] mediaData = null;
FacebookMediaObject medObj = new FacebookMediaObject();
JsonObject p = new JsonObject();
mediaData = _media.bytes;
medObj.SetValue(mediaData);
medObj.FileName = "InteractiveConsole.mp4";
medObj.ContentType = "multipart/form-data";
p.Add("description", message);
p.Add("source", medObj);
uploadedImgID = "";
fbClient.PostCompleted += OnPostImageUploadComplete;
fbClient.PostAsync("/me/videos", p);
//wait for image upload status and hopefully it returned a media ID on successful image upload
while (uploadedImgID == "")
{
//if image updload failed because of rate limit failure we can just abort rest of this
if (rateLimitErrorOccurred)
{
rateLimitErrorOccurred = false;
yield break;
}
yield return true;
}
//if video uploaded succesfully
if (uploadedImgID != null)
{
mediaIDs.Add (uploadedImgID);
var response = (JsonObject)fbClient.Get("/" + uploadedImgID + "?fields=status,published");
TruLogger.LogError("Video Status Details: " + response.ToString());
string vidStatus = (response["status"] as IDictionary<string, object>)["video_status"] as string;
while( vidStatus != "ready")
{
yield return new WaitForSeconds(5.0f);
response = (JsonObject)fbClient.Get("/" + uploadedImgID + "?fields=status,published");
TruLogger.LogError("Video Status Details: " + response.ToString());
vidStatus = (response["status"] as IDictionary<string, object>)["video_status"] as string;
}
TruLogger.LogError("Video ready");
yield return new WaitForSeconds(5.0f);
}
}
if (mediaIDs.Count > 0)
{
curPage = m_UserPages[FacebookFeedsList.GetInstance().ActiveFeed] as IDictionary<string, object>;
fbClient = new FacebookClient(curPage["access_token"].ToString());
JsonObject p = new JsonObject();
p.Add("message", message);
for (int i = 0; i < mediaIDs.Count; i++)
{
p.Add ("attached_media["+ i + "]", "{\"media_fbid\":\"" + mediaIDs[i] + "\"}");
}
fbClient.PostCompleted += OnPostUploadComplete;
fbClient.PostAsync("/me/feed", p);
}
}
void OnPostImageUploadComplete(object sender, FacebookApiEventArgs args)
{
//if successful store resulting ID of image on Facebook
try
{
var json = args.GetResultData<JsonObject>();
uploadedImgID = json["id"].ToString();
//TruLogger.LogError(json.ToString());
}
catch (Exception e)
{
apiPostStatusMessage = "FB post failed: media upload error";
TruLogger.LogError (e.Message);
TruLogger.LogError (e.InnerException.ToString());
FacebookOAuthException oEx = (FacebookOAuthException)(e.InnerException);
if (oEx != null) {
TruLogger.LogError ("Error Type: " + oEx.ErrorType);
TruLogger.LogError ("Error Code: " + oEx.ErrorCode);
TruLogger.LogError ("Error Subcode: " + oEx.ErrorSubcode);
apiAbortErrorCode = oEx.ErrorCode;
if (apiAbortErrorCode == 32 || apiAbortErrorCode == 4)
{
rateLimitErrorOccurred = true;
apiPostAbortErrorMessage = "WHOOPS! Looks like you have exceeded the daily API rate quota for one or more of your feeds. You may have to wait between 1-24 hours until Facebook replenishes your balance for these feeds: \n\n" + e.InnerException.ToString ();
}
else
{
rateLimitErrorOccurred = false;
apiPostAbortErrorMessage = "WHOOPS! Looks like your session has expired or become invalid. Try Signing in again to revalidate your session: \n\n" + e.InnerException.ToString ();
}
}
else
{
rateLimitErrorOccurred = false;
apiAbortErrorCode = -1;
apiPostAbortErrorMessage = "WHOOPS! Looks like your session has expired or become invalid. Try Signing in again to revalidate your session: \n\n" + e.InnerException.ToString ();
}
uploadedImgID = null;
}
}
//generic call back for any post calls
void OnPostUploadComplete(object sender, FacebookApiEventArgs args)
{
try
{
var json = args.GetResultData();
TruLogger.LogError(json.ToString());
apiPostStatusMessage = "FB post successfully submitted";
//FacebookPostStatusUpdatedEvent.Invoke("FB post successfully submitted");
}
catch (Exception e)
{
apiPostStatusMessage = "FB post failed to submit";
//FacebookPostStatusUpdatedEvent.Invoke("FB post failed to submit");
TruLogger.LogError (e.Message);
TruLogger.LogError (e.InnerException.ToString());
FacebookOAuthException oEx = (FacebookOAuthException)(e.InnerException);
if (oEx != null) {
TruLogger.LogError ("Error Type: " + oEx.ErrorType);
TruLogger.LogError ("Error Code: " + oEx.ErrorCode);
TruLogger.LogError ("Error Subcode: " + oEx.ErrorSubcode);
apiAbortErrorCode = oEx.ErrorCode;
if (apiAbortErrorCode == 32 || apiAbortErrorCode == 4)
{
rateLimitErrorOccurred = true;
apiPostAbortErrorMessage = "WHOOPS! Looks like you have exceeded the daily API rate quota for one or more of your feeds. You may have to wait between 1-24 hours until Facebook replenishes your balance for these feeds: \n\n" + e.InnerException.ToString ();
}
else
{
rateLimitErrorOccurred = false;
apiPostAbortErrorMessage = "WHOOPS! Looks like your session has expired or become invalid. Try Signing in again to revalidate your session: \n\n" + e.InnerException.ToString ();
}
}
else
{
rateLimitErrorOccurred = false;
apiAbortErrorCode = -1;
apiPostAbortErrorMessage = "WHOOPS! Looks like your session has expired or become invalid. Try Signing in again to revalidate your session: \n\n" + e.InnerException.ToString ();
}
}
}
Facebook doesn't directly allow to upload multiple videos or photos with videos on the business page. However, it is possible on a personal page so as an alternate solution you can create a post on a personal page and share it on the business page.
Check this video for more information: https://www.youtube.com/watch?v=AoK_1S71q1o

Using websockets through the simple websockets for webgl asset in unity3d, can connect but can't transmit messages

Just having a problem on my mac trying to send strings over web sockets using this https://www.assetstore.unity3d.com/en/#!/content/38367
Lots of adapted code below from here mainly http://www.codepool.biz/how-to-implement-a-java-websocket-server-for-image-transmission-with-jetty.html and the web socket sharp echotest example.
I can connect but there is no sign of strings in my Jetty server console window (on a ws server running in java(eclipse)).
Iā€™m basically just trying to send a ā€œ1ā€ to my server over a websocket connection with the unity editor (5) at the moment, to prompt the server to start sending PNG files encoded as byte arrays, so I can put them back together in a C# script and apply them to a texture.
this is the script, I want to attach it to a game object like a plane or a cube and display the updating images sent over the web socket from my Jetty server, but at the moment I'm just stuck trying to send a message and see it pop up in my eclipse console window.
using UnityEngine;
using System.Collections;
using System;
public class socketTexture : MonoBehaviour {
// Use this for initialization
IEnumerator Start () {
WebSocket w = new WebSocket(new Uri("ws://192.168.0.149:8080/"));
yield return StartCoroutine(w.Connect());
Debug.Log ("Connected");
w.SendString("I'm client");
w.SendString("1");
while (true)
{
byte[] reply = w.Recv();
if (reply != null)
{
Debug.Log ("Received: "+reply);
var tex = new Texture2D(300, 300, TextureFormat.PVRTC_RGBA4, false);
// Load data into the texture and upload it to the GPU.
tex.LoadRawTextureData(reply);
tex.Apply();
// Assign texture to renderer's material.
GetComponent<Renderer>().material.mainTexture = tex;
}
if (w.Error != null)
{
Debug.LogError ("Error: "+w.Error);
break;
}
yield return 0;
}
w.Close();
}
}
...And the relevant code from the jetty server, but this works, I've tested it with some javascript and I can load the PNGs back into the browser window, so I'm definitely doing something wrong in Unity
#OnWebSocketMessage //part request from websocket client (remote browser)
public void onMessage( String message) {
System.out.println("message");
if (message.equals("1") || message.equals("2") || message.equals("3") || message.equals("4") ) {
System.out.println("Part " + message + " joined");
System.out.println( UIMain.usersPath + "/" + message + ".png" );
final String testVar = ( UIMain.usersPath + "/" + message + ".png" );
task = new FileWatcher( new File(testVar) ) {
protected void onChange( File file ) {
// here we code the action on a change
System.out.println( "File "+ file.getName() +" has changed!" );
try {
File f = new File(testVar);
BufferedImage bi = ImageIO.read(f);
ByteArrayOutputStream out = new ByteArrayOutputStream();
ImageIO.write(bi, "png", out);
ByteBuffer byteBuffer = ByteBuffer.wrap(out.toByteArray());
mSession.getRemote().sendBytes(byteBuffer);
out.close();
byteBuffer.clear();
}
catch (IOException e) {
e.printStackTrace();
}
}
};
Timer timer1 = new Timer(); {
timer1.schedule(task , new Date(), 40 );
}
}
else if (message.equals( "0")) {
zerocounter = zerocounter + 1;
if (zerocounter >= 2) {
task.cancel();
}
}
else if (message.equals( "Hi there, client here")) {
System.out.println( "Client says: " + message );
}
}
Any help would be really appreciated, been lurking on here for years, hopefully getting to the stage soon where I can help out others a bit too.
Benedict
Edit:
This is my console error message in Unity
FormatException: Invalid length. System.Convert.FromBase64String
(System.String s) (at
/Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System/Convert.cs:146)
EchoTest+c__Iterator0.MoveNext () (at
Assets/Example/EchoTest.cs:11)
I'm pretty sure the problem arises from websocket sharp for webgl. I need to send the message as a byte array.
OK So Joakim Erdfelt was right, the server was not configured to handle Byte[] messages. Here's what I added to fix it:
#OnWebSocketMessage
public void onMessage(byte[] buffer, int offset, int length) throws UnsupportedEncodingException {
System.out.println(buffer);
String sFclientOutStr = new String(buffer, "UTF-8");
sFclientOut = Integer.parseInt(sFclientOutStr);
System.out.println(sFclientOut);
if ((sFclientOut > 0) & (sFclientOut < 500)) {
System.out.println("Part " + sFclientOut + " joined");
System.out.println( UIMain.usersPath + "/" + sFclientOutStr + ".png" );
final String testVar = ( UIMain.usersPath + "/" + sFclientOutStr + ".png" );
task = new FileWatcher( new File(testVar) ) {
protected void onChange( File file ) {
// here we code the action on a change
System.out.println( "File "+ file.getName() +" has changed!" );
try {
File f = new File(testVar);
BufferedImage bi = ImageIO.read(f);
ByteArrayOutputStream out = new ByteArrayOutputStream();
ImageIO.write(bi, "png", out);
ByteBuffer byteBuffer = ByteBuffer.wrap(out.toByteArray());
mSession.getRemote().sendBytes(byteBuffer);
out.close();
byteBuffer.clear();
}
catch (IOException e) {
e.printStackTrace();
}
}
};
Timer timer1 = new Timer(); {
timer1.schedule(task , new Date(), 40 );
}
}
else if (sFclientOutStr.equals("0")) {
zerocounter = zerocounter + 1;
if (zerocounter >= 2) {
task.cancel();
}
}
else if (sFclientOutStr.equals( "I'm client")) {
System.out.println( "Client says: " + sFclientOutStr );
}
}
These links helped explain it for me http://www.programcreek.com/java-api-examples/index.php?api=org.eclipse.jetty.websocket.api.annotations.OnWebSocketMessage
http://www.eclipse.org/jetty/documentation/current/jetty-websocket-api-annotations.html

How to load Asset bundle scene in unity 4

I have made asset bundle of a scene like this
[MenuItem("MFKJ/BuildSceneAsset")]
static void myBuild() {
string[] levels = { "Assets/Scene/Scene2.unity" };
string v = BuildPipeline.BuildStreamedSceneAssetBundle(levels, "Assets/AssetBundles/Streamed-Level2.unity3d", BuildTarget.StandaloneWindows);
Debug.Log(v);
//"Streamed-Level1.unity3d"
//BuildPipeline.BuildStreamedSceneAssetBundle(levels, "Streamed-Level1.unity3d", BuildTarget.StandaloneWindows64);
}
This has made a .unity3d assetbundle file in specified folder.
Now on run time i am loading the scene from asset bundle form another scene using this code.
public string url;
// Use this for initialization
void Start () {
url = "file://" + Application.dataPath + "/AssetBundles/Streamed-Level2.unity3d";
Debug.Log("scene load url : " + url);
StartCoroutine(LoadSceneBundle());
}
// Update is called once per frame
void Update () {
}
public IEnumerator LoadSceneBundle() {
using (WWW www = WWW.LoadFromCacheOrDownload(url,1)){
yield return www;
if (www.error != null) {
throw new Exception("WWW donwload had an error : " + url + " " + www.error);
//Debug.Log("");
}
AssetBundle ab = www.assetBundle;
Debug.Log(www.assetBundle.mainAsset);
//GameObject gm = ab.mainAsset as GameObject;
//Debug.Log("Scene2PassValue : " + gm.name);
//Instantiate(gm);
//ab.LoadAll(typeof(GameObject));
ab.LoadAll();
ab.Unload(false);
}
}
but unfortunately this is not loading the streamed scene or any asset of streamed scene from asset bundle. what i am missing?