Update Asset
Updating assets in Storyblok is very similar to uploading assets, however here you need to specify the ID
of the asset you want to update alongside the new asset data. Also, you need to pass a new key and value pair "validate_upload":"1"
to the request.
spaces/:space_id/assets/:asset_id
Path Parameters
-
:space_id
numberNumeric id of a space
-
:asset_id
stringNumeric id of an asset
Request Body Properties
-
id
numberThe numeric ID
1. Start by requesting a signed upload URL and parameter
Example Request
Request
curl "https://mapi.storyblok.com/v1/spaces/656/assets/839450823" \
-X PUT \
-H "Authorization: YOUR_OAUTH_TOKEN" \
-H "Content-Type: application/json" \
-d "{\"filename\": \"your_file.jpg\",\"size\": \"400x500\"}"
Request
// Using the Universal JavaScript Client:
// https://github.com/storyblok/storyblok-js-client
Storyblok.put('/spaces/656/assets/839450823', {
"filename": "your_file.jpg",
"size": "400x500"
})
.then(response => {
console.log(response)
}).catch(error => {
console.log(error)
})
Request
$client = new \Storyblok\Client('YOUR_STORYBLOK_SPACE_ACCESS_TOKEN');
$payload = [
"filename" => "your_file.jpg",
"size" => "400x500"
];
$client->put('/spaces/656/assets/839450823', $payload)->getBody();
Request
require 'storyblok'
client = Storyblok::Client.new(oauth_token: 'YOUR_OAUTH_TOKEN')
payload = {
"filename" => "your_file.jpg",
"size" => "400x500"
}
client.put('/spaces/656/assets/839450823', payload)
Request
HttpResponse<String> response = Unirest.put("https://mapi.storyblok.com/v1/spaces/656/assets/839450823")
.header("Content-Type", "application/json")
.header("Authorization", "YOUR_OAUTH_TOKEN")
.body("{\"filename\": \"your_file.jpg\",\"size\": \"400x500\"}")
.asString();
Request
var client = new RestClient("https://mapi.storyblok.com/v1/spaces/656/assets/839450823");
var request = new RestRequest(Method.PUT);
request.AddHeader("Content-Type", "application/json");
request.AddHeader("Authorization", "YOUR_OAUTH_TOKEN");
request.AddParameter("application/json", "{\"filename\": \"your_file.jpg\",\"size\": \"400x500\"}", ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
Request
import Foundation
let headers = [
"Content-Type": "application/json",
"Authorization": "YOUR_OAUTH_TOKEN"
]
let postData = NSData(data: "{\"filename\": \"your_file.jpg\",\"size\": \"400x500\"}".data(using: String.Encoding.utf8)!)
let request = NSMutableURLRequest(url: NSURL(string: "https://mapi.storyblok.com/v1/spaces/656/assets/839450823")! as URL, cachePolicy: .useProtocolCachePolicy, timeoutInterval: 10.0)
request.method = "PUT"
request.allHTTPHeaderFields = headers
request.httpBody = postData as Data
let session = URLSession.shared
let dataTask = session.dataTask(with: request as URLRequest, completionHandler: { (data, response, error) -> Void in
if (error != nil) {
print(error)
} else {
let httpResponse = response as? HTTPURLResponse
print(httpResponse)
}
})
dataTask.resume()
Request
import requests
url = "https://mapi.storyblok.com/v1/spaces/656/assets/839450823"
querystring = {}
payload = "{\"filename\": \"your_file.jpg\",\"size\": \"400x500\"}"
headers = {
'Content-Type': "application/json",
'Authorization': "YOUR_OAUTH_TOKEN"
}
response = requests.request("PUT", url, data=payload, headers=headers, params=querystring)
print(response.text)
Example Request (with asset folder)
Request
curl "https://mapi.storyblok.com/v1/spaces/606/assets/839450823" \
-X PUT \
-H "Authorization: YOUR_OAUTH_TOKEN" \
-H "Content-Type: application/json" \
-d "{\"filename\": \"your_file.jpg\",\"id\": \"16383\",\"validate_upload\": \"1\",\"asset_folder_id\":123}"
Request
// Using the Universal JavaScript Client:
// https://github.com/storyblok/storyblok-js-client
Storyblok.put('/spaces/606/assets/839450823', {
"filename": "your_file.jpg",
"id": "16383",
"validate_upload": "1",
"asset_folder_id":123
})
.then(response => {
console.log(response)
}).catch(error => {
console.log(error)
})
Request
$client = new \Storyblok\Client('YOUR_STORYBLOK_SPACE_ACCESS_TOKEN');
$payload = [
"filename" => "your_file.jpg",
"id" => "16383",
"validate_upload" => "1",
"asset_folder_id" => 123
];
$client->put('/spaces/606/assets/839450823', $payload)->getBody();
Request
require 'storyblok'
client = Storyblok::Client.new(oauth_token: 'YOUR_OAUTH_TOKEN')
payload = {
"filename" => "your_file.jpg",
"id" => "16383",
"validate_upload" => "1",
"asset_folder_id" => 123
}
client.put('/spaces/606/assets/839450823', payload)
Request
HttpResponse<String> response = Unirest.put("https://mapi.storyblok.com/v1/spaces/606/assets/839450823")
.header("Content-Type", "application/json")
.header("Authorization", "YOUR_OAUTH_TOKEN")
.body("{\"filename\": \"your_file.jpg\",\"id\": \"16383\",\"validate_upload\": \"1\",\"asset_folder_id\":123}")
.asString();
Request
var client = new RestClient("https://mapi.storyblok.com/v1/spaces/606/assets/839450823");
var request = new RestRequest(Method.PUT);
request.AddHeader("Content-Type", "application/json");
request.AddHeader("Authorization", "YOUR_OAUTH_TOKEN");
request.AddParameter("application/json", "{\"filename\": \"your_file.jpg\",\"id\": \"16383\",\"validate_upload\": \"1\",\"asset_folder_id\":123}", ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
Request
import Foundation
let headers = [
"Content-Type": "application/json",
"Authorization": "YOUR_OAUTH_TOKEN"
]
let postData = NSData(data: "{\"filename\": \"your_file.jpg\",\"id\": \"16383\",\"validate_upload\": \"1\",\"asset_folder_id\":123}".data(using: String.Encoding.utf8)!)
let request = NSMutableURLRequest(url: NSURL(string: "https://mapi.storyblok.com/v1/spaces/606/assets/839450823")! as URL, cachePolicy: .useProtocolCachePolicy, timeoutInterval: 10.0)
request.method = "PUT"
request.allHTTPHeaderFields = headers
request.httpBody = postData as Data
let session = URLSession.shared
let dataTask = session.dataTask(with: request as URLRequest, completionHandler: { (data, response, error) -> Void in
if (error != nil) {
print(error)
} else {
let httpResponse = response as? HTTPURLResponse
print(httpResponse)
}
})
dataTask.resume()
Request
import requests
url = "https://mapi.storyblok.com/v1/spaces/606/assets/839450823"
querystring = {}
payload = "{\"filename\": \"your_file.jpg\",\"id\": \"16383\",\"validate_upload\": \"1\",\"asset_folder_id\":123}"
headers = {
'Content-Type': "application/json",
'Authorization': "YOUR_OAUTH_TOKEN"
}
response = requests.request("PUT", url, data=payload, headers=headers, params=querystring)
print(response.text)
2. Use the received signed response object to upload your file (example uses Node.js) and to finalize the process:
const FormData = require('form-data')
const fs = require('fs')
const file = '/path_to/your_file.jpg'
const fileUpload = (signed_response_object, success, failed) => {
let form = new FormData()
// apply all fields from the signed response object to the second request
for (let key in signed_response_object.fields) {
form.append(key, signed_response_object.fields[key])
}
// also append the file read stream
form.append('file', fs.createReadStream(file))
// submit your form
form.submit(signed_response_object.post_url, (err, res) => {
if (err) throw err
// 3. finalize the upload
Storyblok.get('spaces/606/assets/' + signed_response_object.id + '/finish_upload').then(response => {
console.log('https://a.storyblok.com/' + signed_response_object.fields.key + ' uploaded!')
}).catch(error => {
throw error
})
console.log('https://a.storyblok.com/' + signed_response_object.fields.key + ' uploaded!')
})
}
Storyblok.put('spaces/656/assets/', {
filename: 'new_file.jpg',
id: '18472',
validate_upload: '1'
}).then(response => {
console.log(response)
}).catch(error => {
throw error
})