{"__v":7,"_id":"5735d367a430ed22001c5e85","category":{"project":"54774d9af3736008009e9e0e","version":"56a0bd9e3697d80d002ac5e7","_id":"5735a52431a73b1700887ca0","__v":0,"sync":{"url":"","isSync":false},"reference":false,"createdAt":"2016-05-13T09:57:56.274Z","from_sync":false,"order":13,"slug":"data-objects","title":"Data Objects"},"parentDoc":null,"project":"54774d9af3736008009e9e0e","user":"54774c47f3736008009e9e0c","version":{"__v":20,"_id":"56a0bd9e3697d80d002ac5e7","project":"54774d9af3736008009e9e0e","createdAt":"2016-01-21T11:14:38.131Z","releaseDate":"2016-01-21T11:14:38.131Z","categories":["56a0bd9f3697d80d002ac5e8","56a0bd9f3697d80d002ac5e9","56a0bd9f3697d80d002ac5ea","56a0bd9f3697d80d002ac5eb","56b098a43a5b810d00745e3f","56b098da8f7a4f0d0029dd10","56d5a8ecf612b80b00fb69cd","570989853ab43c0e0072b2d6","571006aa10300c0e007f6133","5735a52431a73b1700887ca0","5744b0529e045c0e00b7a7a2","5744b1d0b56d431700d8a4fb","5744b1fb55d65a0e00b436e1","5744b218f9c7411700ce560e","5744b220f9c7411700ce560f","5744b2259e045c0e00b7a7a7","5744b22cf8b79f0e00de6a2d","5744b23155d65a0e00b436e4","5744b242c758290e00fbc235","5744b2569e045c0e00b7a7a8","5756a3f2bb92962900dafe3d","57ee23e81e42900e0014a42f","581c822c98676e0f00d240ef"],"is_deprecated":false,"is_hidden":false,"is_beta":false,"is_stable":true,"codename":"Sockets","version_clean":"1.1.0","version":"1.1"},"updates":[],"next":{"pages":[],"description":""},"createdAt":"2016-05-13T13:15:19.268Z","link_external":false,"link_url":"","githubsync":"","sync_unique":"","hidden":false,"api":{"results":{"codes":[]},"settings":"","auth":"required","params":[],"url":""},"isReference":false,"order":4,"body":"## Chapter contents\n\n1. [Creating a Data Object with `file` property](#creating-a-data-object-with-file-property)\n2. [Summary](#summary)\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Creating a Data Object with `file` property\"\n}\n[/block]\nAdding Data Objects with the `file` property is a special case. First, make sure you've created a Data Class with a `file` field type in the Data Class Schema.\n\nOnce you have done that, this is how creating a Data Object with a file looks like:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"curl -X POST \\\\\\n-H \\\"Content-type: multipart/form-data\\\" \\\\\\n-H \\\"X-API-KEY: API_KEY\\\" \\\\\\n-F \\\"file=:::at:::PATH_TO_FILE\\\" \\\\\\n\\\"https://api.syncano.io/v1.1/instances/instance_name/classes/DATA_CLASS_NAME/objects/\\\"\",\n      \"language\": \"curl\"\n    },\n    {\n      \"code\": \"import syncano\\nfrom syncano.models import Object\\n\\nconnection = syncano.connect(api_key='API_KEY')\\n\\nimage_path = \\\"/sample_path/image.jpg\\\"\\nimage_file = open(image_path, \\\"r\\\")\\nObject.please.create(\\n  instance_name=\\\"INSTANCE_NAME\\\",\\n  class_name=\\\"CLASS_NAME\\\",\\n  image=image_file\\n)\",\n      \"language\": \"python\"\n    },\n    {\n      \"code\": \"var Syncano = require(\\\"syncano\\\");  // CommonJS\\nvar connection = Syncano({accountKey: \\\"ACCOUNT_KEY\\\"});\\nvar DataObject = connection.DataObject;\\n\\n// To upload your file, you have to use the `files` property of the input and attach it to the object via `Syncano.file` method. Do this during the submission of the form. \\nvar object = {\\n  file_field: Syncano.file(input.files[0]),\\n  instanceName: \\\"INSTANCE_NAME\\\",\\n  className: \\\"CLASS_name\\\"\\n};\\n\\n// To upload files from the server environment, use the `fs`and `Syncano.file` method for attaching the file to your object.\\n\\nvar object = {\\n  file_field_name: Syncano.file(dirname__ + \\\"/path_to_my_file.txt\\\"),\\n  instanceName: \\\"INSTANCE_NAME\\\",\\n  className: \\\"CLASS_name\\\"\\n};\\n\\nDataObject.please().create(object).then(function(object) {\\n\\tconsole.log(\\\"object\\\", object);\\n});\",\n      \"language\": \"javascript\"\n    },\n    {\n      \"code\": \"Book bookWithCover = new Book();\\nbookWithCover.author = \\\"Ernest Hemingway\\\";\\nbookWithCover.title = \\\"The Old Man and the Sea\\\";\\nbookWithCover.cover = new SyncanoFile(new File(getAssetsDir(), \\\"blue.png\\\"));\\n\\nResponse<Book> response = bookWithCover.save();\",\n      \"language\": \"java\",\n      \"name\": \"Android\"\n    },\n    {\n      \"code\": \"#Data Objects with file property\\n\\n@interface Book : SCDataObject\\n@property (nonatomic,retain) SCFile *cover;\\n@end\\n\\n#Creating and saving file\\n\\nSCFile *cover = [SCFile fileWithaData:[NSData dataWithContentsOfFile:@\\\"FILE_PATH\\\"]];\\nBook *book = [Book new];\\nbook.cover = cover;\\n[book saveWithCompletionBlock:^(NSError * _Nullable error) {\\n   //Handle error\\n}];\\n\",\n      \"language\": \"objectivec\"\n    },\n    {\n      \"code\": \"#Data Objects with file property\\n\\nclass Book: SCDataObject {\\n    var cover:SCFile?\\n}\\n\\n#Creating and saving file\\n\\ndo {\\n    let cover:SCFile = try SCFile(aData: NSData(contentsOfFile: \\\"FILE_PATH\\\") as Data)\\n    let book:Book = Book()\\n    book.cover = cover\\n    book.save(completionBlock: { (error) in\\n        //Handle error\\n    })\\n} catch {\\n    //Handle error\\n}\",\n      \"language\": \"swift\"\n    },\n    {\n      \"code\": \"// NOT AVAIABLE\",\n      \"language\": \"csharp\",\n      \"name\": \"Unity\"\n    }\n  ]\n}\n[/block]\n\n[block:callout]\n{\n  \"type\": \"info\",\n  \"body\": \"Current limit for file size that can be stored on Syncano is 128MB.\"\n}\n[/block]\nIn the response, the `file` property will have to fields:\n- type\n- value\n\nValue field is a url. The resource that you just uploaded is being stored under this url. \n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"{\\n    \\\"channel\\\": null,\\n    \\\"channel_room\\\": null,\\n    \\\"created_at\\\": \\\"2015-07-22T15:21:23.388062Z\\\",\\n    \\\"file\\\": {\\n        \\\"type\\\": \\\"file\\\",\\n        \\\"value\\\": \\\"https://d3rij3t703q5l6.cloudfront.net/305/10/523956605dbc303fa74e06061f40ccf1ce1ea978.txt\\\"\\n    },\\n    \\\"group\\\": null,\\n    \\\"group_permissions\\\": \\\"none\\\",\\n    \\\"id\\\": 29,\\n    \\\"links\\\": {\\n        \\\"self\\\": \\\"/v1.1/instances/instance_name/classes/class/objects/29/\\\"\\n    },\\n    \\\"other_permissions\\\": \\\"none\\\",\\n    \\\"owner\\\": null,\\n    \\\"owner_permissions\\\": \\\"none\\\",\\n    \\\"revision\\\": 1,\\n    \\\"string\\\": null,\\n    \\\"updated_at\\\": \\\"2015-07-22T15:21:23.388100Z\\\"\\n}\",\n      \"language\": \"json\"\n    }\n  ]\n}\n[/block]\n\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Summary\"\n}\n[/block]\nGreat! Now you know the how to upload files to Syncano. Next chapter will be about adding spacial coordinates to your Data Objects, which are called [GeoPoints](doc:geopoints).","excerpt":"In this chapter you will learn:\n- What are Data Objects\n- What's inside a Data Object\n- How to perform different operations on Data Objects","slug":"handling-files","type":"basic","title":"Handling files"}

Handling files

In this chapter you will learn: - What are Data Objects - What's inside a Data Object - How to perform different operations on Data Objects

## Chapter contents 1. [Creating a Data Object with `file` property](#creating-a-data-object-with-file-property) 2. [Summary](#summary) [block:api-header] { "type": "basic", "title": "Creating a Data Object with `file` property" } [/block] Adding Data Objects with the `file` property is a special case. First, make sure you've created a Data Class with a `file` field type in the Data Class Schema. Once you have done that, this is how creating a Data Object with a file looks like: [block:code] { "codes": [ { "code": "curl -X POST \\\n-H \"Content-type: multipart/form-data\" \\\n-H \"X-API-KEY: API_KEY\" \\\n-F \"file=@PATH_TO_FILE\" \\\n\"https://api.syncano.io/v1.1/instances/instance_name/classes/DATA_CLASS_NAME/objects/\"", "language": "curl" }, { "code": "import syncano\nfrom syncano.models import Object\n\nconnection = syncano.connect(api_key='API_KEY')\n\nimage_path = \"/sample_path/image.jpg\"\nimage_file = open(image_path, \"r\")\nObject.please.create(\n instance_name=\"INSTANCE_NAME\",\n class_name=\"CLASS_NAME\",\n image=image_file\n)", "language": "python" }, { "code": "var Syncano = require(\"syncano\"); // CommonJS\nvar connection = Syncano({accountKey: \"ACCOUNT_KEY\"});\nvar DataObject = connection.DataObject;\n\n// To upload your file, you have to use the `files` property of the input and attach it to the object via `Syncano.file` method. Do this during the submission of the form. \nvar object = {\n file_field: Syncano.file(input.files[0]),\n instanceName: \"INSTANCE_NAME\",\n className: \"CLASS_name\"\n};\n\n// To upload files from the server environment, use the `fs`and `Syncano.file` method for attaching the file to your object.\n\nvar object = {\n file_field_name: Syncano.file(dirname__ + \"/path_to_my_file.txt\"),\n instanceName: \"INSTANCE_NAME\",\n className: \"CLASS_name\"\n};\n\nDataObject.please().create(object).then(function(object) {\n\tconsole.log(\"object\", object);\n});", "language": "javascript" }, { "code": "Book bookWithCover = new Book();\nbookWithCover.author = \"Ernest Hemingway\";\nbookWithCover.title = \"The Old Man and the Sea\";\nbookWithCover.cover = new SyncanoFile(new File(getAssetsDir(), \"blue.png\"));\n\nResponse<Book> response = bookWithCover.save();", "language": "java", "name": "Android" }, { "code": "#Data Objects with file property\n\n@interface Book : SCDataObject\n@property (nonatomic,retain) SCFile *cover;\n@end\n\n#Creating and saving file\n\nSCFile *cover = [SCFile fileWithaData:[NSData dataWithContentsOfFile:@\"FILE_PATH\"]];\nBook *book = [Book new];\nbook.cover = cover;\n[book saveWithCompletionBlock:^(NSError * _Nullable error) {\n //Handle error\n}];\n", "language": "objectivec" }, { "code": "#Data Objects with file property\n\nclass Book: SCDataObject {\n var cover:SCFile?\n}\n\n#Creating and saving file\n\ndo {\n let cover:SCFile = try SCFile(aData: NSData(contentsOfFile: \"FILE_PATH\") as Data)\n let book:Book = Book()\n book.cover = cover\n book.save(completionBlock: { (error) in\n //Handle error\n })\n} catch {\n //Handle error\n}", "language": "swift" }, { "code": "// NOT AVAIABLE", "language": "csharp", "name": "Unity" } ] } [/block] [block:callout] { "type": "info", "body": "Current limit for file size that can be stored on Syncano is 128MB." } [/block] In the response, the `file` property will have to fields: - type - value Value field is a url. The resource that you just uploaded is being stored under this url. [block:code] { "codes": [ { "code": "{\n \"channel\": null,\n \"channel_room\": null,\n \"created_at\": \"2015-07-22T15:21:23.388062Z\",\n \"file\": {\n \"type\": \"file\",\n \"value\": \"https://d3rij3t703q5l6.cloudfront.net/305/10/523956605dbc303fa74e06061f40ccf1ce1ea978.txt\"\n },\n \"group\": null,\n \"group_permissions\": \"none\",\n \"id\": 29,\n \"links\": {\n \"self\": \"/v1.1/instances/instance_name/classes/class/objects/29/\"\n },\n \"other_permissions\": \"none\",\n \"owner\": null,\n \"owner_permissions\": \"none\",\n \"revision\": 1,\n \"string\": null,\n \"updated_at\": \"2015-07-22T15:21:23.388100Z\"\n}", "language": "json" } ] } [/block] [block:api-header] { "type": "basic", "title": "Summary" } [/block] Great! Now you know the how to upload files to Syncano. Next chapter will be about adding spacial coordinates to your Data Objects, which are called [GeoPoints](doc:geopoints).