Reputation: 6868
I am trying to save a jpeg that I get back from Microsoft's Graph API to an S3 bucket (Digital Ocean Space actually but they work the same). I am able to get the image binary from the graph api. And can upload it successfully. But the image is gibberish at the endpoint. Here is the code I am using for reference.
// Prep to fetch user photo from microsoft office 365
// using recently aquired access token
const photoEndpoint = `${MS_GRAPH_URL}/v1.0/me/photo/$value`;
const config = { headers: { Authorization: `Bearer ${access_token}` } };
// Fetch user photo from microsoft office 365
const { data: photo } = await axios.get(photoEndpoint, {
headers: { Authorization: `Bearer ${access_token}` }
});
await storeImage(photo, id);
where storeImage
is defined as follows:
// Import AWS SDK
const AWS = require("aws-sdk");
// Instantiate s3 instance to iterface with digital ocean spaced
const spacesEndpoint = new AWS.Endpoint("sfo2.digitaloceanspaces.com");
const s3 = new AWS.S3({
endpoint: spacesEndpoint,
accessKeyId: process.env.DO_SPACE_KEY,
secretAccessKey: process.env.DO_SPACE_SECRET
});
const storeImage = (image, key) => {
const params = {
ACL: "public-read",
Bucket: process.env.DO_SPACE_BUCKET,
Key: `${key}`,
Body: image,
ContentType: "image/jpeg"
};
const promise = new Promise((resolve, reject) => {
s3.upload(params, (err, data) => {
if (err) reject(err);
resolve(data);
});
});
return promise;
};
Any thoughts on what I am doing wrong?
Upvotes: 0
Views: 1122
Reputation: 17367
Where do you indicate the Content-Type of data you're requesting via your axios
call?
The default appears to be application/json
rather than binary/octet-stream
so your image data will undergo unwanted transformation. ;)
Adding responseType: 'stream'
to your axios request should fix your issue.
Upvotes: 1