0
0
Expressframework~20 mins

Cloud storage integration concept in Express - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Cloud Storage Integration Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What happens when uploading a file using Express with Multer and AWS S3?

Consider this Express route that uses Multer middleware to handle file uploads and then uploads the file to AWS S3. What is the expected behavior after a successful upload?

Express
const express = require('express');
const multer = require('multer');
const AWS = require('aws-sdk');
const app = express();
const upload = multer({ storage: multer.memoryStorage() });
const s3 = new AWS.S3();

app.post('/upload', upload.single('file'), async (req, res) => {
  const params = {
    Bucket: 'my-bucket',
    Key: req.file.originalname,
    Body: req.file.buffer
  };
  try {
    await s3.upload(params).promise();
    res.status(200).send('File uploaded successfully');
  } catch (err) {
    res.status(500).send('Upload failed');
  }
});
AThe file is stored in memory temporarily, then uploaded to the S3 bucket with the original filename, and the client receives a success message.
BThe file is saved to disk first, then uploaded to S3, and the client receives the file URL in the response.
CThe file is uploaded directly to S3 without being processed by Multer, and the client receives an error.
DThe file is stored in memory but never uploaded to S3, and the client receives a timeout error.
Attempts:
2 left
💡 Hint

Think about how Multer's memoryStorage works and what the AWS S3 upload method expects.

📝 Syntax
intermediate
2:00remaining
Identify the syntax error in this Express route integrating Google Cloud Storage

Which option correctly fixes the syntax error in this Express route that uploads a file to Google Cloud Storage?

Express
const express = require('express');
const {Storage} = require('@google-cloud/storage');
const multer = require('multer');
const app = express();
const storage = new Storage();
const bucket = storage.bucket('my-bucket');
const upload = multer({ storage: multer.memoryStorage() });

app.post('/upload', upload.single('file'), async (req, res) => {
  const blob = bucket.file(req.file.originalname);
  const blobStream = blob.createWriteStream();

  blobStream.on('error', (err) => {
    res.status(500).send(err.message);
  });

  blobStream.on('finish', () => {
    res.status(200).send('Upload complete');
  });

  blobStream.end(req.file.buffer);
});
AAdd a return statement before res.status(200).send('Upload complete') to fix the missing return error.
BChange blobStream.end(req.file.buffer) to blobStream.write(req.file.buffer) to fix the stream usage.
CAdd a semicolon after bucket.file(req.file.originalname) to fix the missing semicolon error.
DAdd async keyword to the callback function to fix the missing async error.
Attempts:
2 left
💡 Hint

Look carefully at the line that creates the blob variable.

🔧 Debug
advanced
2:00remaining
Why does this Express app fail to upload files to Azure Blob Storage?

Given this Express route, why does the file upload to Azure Blob Storage fail with a 500 error?

Express
const express = require('express');
const multer = require('multer');
const { BlobServiceClient } = require('@azure/storage-blob');
const app = express();
const upload = multer({ storage: multer.memoryStorage() });
const blobServiceClient = BlobServiceClient.fromConnectionString(process.env.AZURE_CONN_STRING);
const containerClient = blobServiceClient.getContainerClient('my-container');

app.post('/upload', upload.single('file'), async (req, res) => {
  try {
    const blockBlobClient = containerClient.getBlockBlobClient(req.file.originalname);
    await blockBlobClient.upload(req.file.buffer, req.file.buffer.length);
    res.status(200).send('Upload successful');
  } catch (error) {
    res.status(500).send('Upload failed');
  }
});
AThe container name 'my-container' is invalid and causes the upload to fail.
BThe upload method requires the buffer length as a second argument, which is missing.
CMulter's memoryStorage does not provide a buffer, so req.file.buffer is undefined.
DThe BlobServiceClient is not initialized correctly without a SAS token.
Attempts:
2 left
💡 Hint

Check the Azure Blob Storage SDK documentation for the upload method signature.

state_output
advanced
2:00remaining
What is the output of this Express route using AWS S3 upload with callback?

What response does the client receive when this Express route uploads a file to AWS S3 using a callback?

Express
const express = require('express');
const multer = require('multer');
const AWS = require('aws-sdk');
const app = express();
const upload = multer({ storage: multer.memoryStorage() });
const s3 = new AWS.S3();

app.post('/upload', upload.single('file'), (req, res) => {
  const params = {
    Bucket: 'my-bucket',
    Key: req.file.originalname,
    Body: req.file.buffer
  };
  s3.upload(params, (err, data) => {
    if (err) {
      res.status(500).send('Upload error');
    }
  });
  res.status(200).send('Upload started');
});
AThe client immediately receives 'Upload started' before the upload finishes.
BThe client receives 'Upload error' if the upload fails, otherwise 'Upload started'.
CThe client waits until the upload finishes and then receives 'Upload started'.
DThe client never receives a response because res.send is called twice.
Attempts:
2 left
💡 Hint

Consider the asynchronous nature of the s3.upload callback and when res.send is called.

🧠 Conceptual
expert
3:00remaining
Which option best describes the advantage of using signed URLs for cloud storage uploads in Express?

When integrating cloud storage with Express, what is the main benefit of generating signed URLs for clients to upload files directly?

AIt enables the server to store files temporarily before forwarding them to cloud storage.
BIt encrypts the files automatically during upload to enhance security.
CIt allows the Express server to validate file contents before upload to cloud storage.
DIt reduces server load by allowing clients to upload files directly to cloud storage without passing through the Express server.
Attempts:
2 left
💡 Hint

Think about how signed URLs change the upload flow between client, server, and cloud storage.