at
March 21, 2023, 10:55am
1
Hi,
We have had an issue for 2 weeks about uploading images. When we try to upload an image, Dato CMS uploads a blank image without any extension. Normally “https://site-api.datocms.com/job-results/ ” returns a path with image extension. However, now we have a path parameter without file extension.
Hello @at and welcome to the community!
Can you share with us the snippet you are using to upload the files onto Dato?
Thank you!
at
March 21, 2023, 11:43am
3
Hi @m.finamor
Thnx for your response
SendDatoCmsApiRequestWithRetryPolicy uses SendDatoCmsApiRequest
public async Task UploadImage(string fileName, Stream stream)
{
var permissionResult = await this.RequestUploadPermission(fileName);
var res = await this.UploadImageToDatoCms(permissionResult.Url, Method.Put, stream);
if (res.StatusCode == HttpStatusCode.OK)
{
string jobId = await CreateActualUpload(permissionResult.Id);
}
else
{
throw new CmsApiException($"Create Actual Upload Failed! Filename: {fileName}");
}
}
private async Task<CreateUploadImageResponse> RequestUploadPermission(string fileName)
{
var request = "{\"data\": {\"type\": \"upload_request\",\"attributes\": {\"filename\": \"" + fileName + "\"}}}";
var response = await SendDatoCmsApiRequestWithRetryPolicy(request, "upload-requests", Method.Post);
if (response.StatusCode == HttpStatusCode.Created)
{
var jsonObject = JObject.Parse(response.Content);
var result = new CreateUploadImageResponse();
result.Id = jsonObject["data"]["id"].ToString();
result.Type = jsonObject["data"]["type"].ToString();
result.Url = jsonObject["data"]["attributes"]["url"].ToString();
return result;
}
else
{
throw new CmsException(response.Content, new CmsApiException(request));
}
}
private async Task<RestResponse> UploadImageToDatoCms(string url, Method method, Stream stream)
{
var request = new RestRequest(url, method);
request.RequestFormat = DataFormat.None;
if (stream is MemoryStream)
{
request.AddParameter("", ((MemoryStream)stream).ToArray(), ParameterType.RequestBody);
}
else
{
using (MemoryStream ms = new MemoryStream())
{
stream.CopyTo(ms);
request.AddParameter("", ms.ToArray(), ParameterType.RequestBody);
}
}
var response = await _restClientDatoImageUploadApi.ExecuteAsync(request);
if (response.StatusCode == HttpStatusCode.OK)
{
return response;
}
else
{
throw new CmsException(response.Content, new CmsApiException("File Upload Exception"));
}
}
private async Task<string> CreateActualUpload(string path)
{
var request = "{\"data\": {\"type\": \"upload\",\"attributes\": {\"path\": \"" + path + "\"}}}";
var response = await SendDatoCmsApiRequestWithRetryPolicy(request, "uploads", Method.Post);
if (response.StatusCode == HttpStatusCode.Accepted)
{
var jsonObject = JObject.Parse(response.Content);
JToken id = jsonObject["data"]["id"];
return id.ToString();
}
else
{
throw new CmsException(response.Content, new CmsApiException(request));
}
}
private async Task<RestResponse> SendDatoCmsApiRequest(string requestContent, string resource, Method method)
{
var request = new RestRequest(resource, method);
request.AddHeader("Authorization", fullAccsessApiKey);
request.AddHeader("Accept", "application/json");
if (false == string.IsNullOrWhiteSpace(requestContent))
{
request.AddJsonBody(requestContent);
}
var response = await _restClientDatoUploadsApi.ExecuteAsync(request);
try
{
var jsonObject = JObject.Parse(response.Content);
var res = jsonObject["errors"];
if (res != null)
{
var cmsException = new CmsException(response.Content, new CmsApiException(requestContent));
_logger.LogError(cmsException, string.Empty);
response.StatusCode = HttpStatusCode.BadRequest;
}
}
catch
{
if (response.StatusCode != HttpStatusCode.OK || response.StatusCode != HttpStatusCode.Created)
{
var cmsException = new CmsException(response.Content, new CmsApiException(requestContent));
_logger.LogError(cmsException, string.Empty);
}
else
{
throw;
}
}
return response;
}
at
March 21, 2023, 11:45am
4
I also checked the job result
https://site-api.datocms.com/job-results/3bf589d00669074c815c352d
it returns a path without file extension.
at
March 23, 2023, 2:48pm
6
Hi @m.finamor
We found the issue. 2-4 weeks ago we did not need to add parameter name to the file upload on AWS because of a Method type (we used POST) now they need “application/octet-stream” as parameter name. I think it is fixed. Thanks for your kind help