AWS SDK for JavaScript (v2) help for migrating to v3 #5415
Replies: 4 comments 4 replies
-
Hi @dhavalveera, Here are some resources for code examples. Regarding your question, its kind of hard to understand what examples you want to see. Whether a bucket is private / public the method of fetching the data is the same. The only difference is if you want to allow your users to download stuff from your private bucket without signing a request you can do that with a presigned URL. upload to s3: import { S3Client, PutObjectCommand } from '@aws-sdk/client-s3'
// client is created once in the global scope.
const s3Client = new S3Client({
region: process.env.secureRegion,
credentials: {
accessKeyId: process.env.secureAccessKeyId,
secretAccessKey: process.env.secureSecretAccessKey,
}
})
async function uploadFileToS3(){
try {
await s3Client.send(new PutObjectCommand({
Bucket: process.env.unsecureBucketName,
Key: renamedFileWithExtension,
Body: file,
}))
console.log("file upload successful")
} catch (error) {
console.log("there was an error:", error)
}
} Presigned GetObject: import { S3Client,, GetObjectCommand } from '@aws-sdk/client-s3'
import { getSignedUrl } from '@aws-sdk/s3-request-presigner';
// client is created once in the global scope.
const s3Client = new S3Client({
region: process.env.secureRegion,
credentials: {
accessKeyId: process.env.secureAccessKeyId,
secretAccessKey: process.env.secureSecretAccessKey,
}
})
async function getPresignedUrlForThatSameObject(){
try {
const url = await getSignedUrl(s3Client, new GetObjectCommand({
Bucket: process.env.secureBucketName,
Key: keyName,
}))
return url
} catch (error) {
console.log("there was an error:", error)
}
} Let me know if you need anything else. |
Beta Was this translation helpful? Give feedback.
-
Thanks @RanVaknin for your reply & providing me the Example which I was looking for. Sorry for the confusion @RanVaknin the example I referred was like I know how to use AWS SDK for JavaScript with v2 version, but v3 has different way of configuring the S3 Clients and all, that example I was referring to and which you've shared as well. import { S3Client, PutObjectCommand } from '@aws-sdk/client-s3'
// client is created once in the global scope.
const s3Client = new S3Client({
region: process.env.secureRegion,
credentials: {
accessKeyId: process.env.secureAccessKeyId,
secretAccessKey: process.env.secureSecretAccessKey,
}
})
async function uploadFileToS3(){
try {
const response = await s3Client.send(new PutObjectCommand({
Bucket: process.env.unsecureBucketName,
Key: renamedFileWithExtension,
Body: file,
}))
console.log("file upload successful")
} catch (error) {
console.log("there was an error:", error)
}
} in the above shared code, when we store the // The following example uploads an object to a versioning-enabled bucket. The source file is specified using Windows file syntax. S3 returns VersionId of the newly created object.
const input = {
"Body": "HappyFace.jpg",
"Bucket": "examplebucket",
"Key": "HappyFace.jpg"
};
const command = new PutObjectCommand(input);
const response = await client.send(command);
/* response ==
{
"ETag": "\"6805f2cfc46c0f04559748bb039d69ae\"",
"VersionId": "tpf3zF08nBplQK1XLOefGskR7mGDwcDk"
}
*\/
// example id: to-upload-an-object-1481760101010 here I can't see an key value in the response object which contains the URL of the uploaded Object/Image. |
Beta Was this translation helpful? Give feedback.
-
Hi @dhavalveera ,
No, the The URL you are after is probably the presigned URL (the second example I provided) and you are presigning it with a get command. I suggest you read up on the documentation for Presigned URLs and get/put objects since your questions are more about the API rather than the SDK. Since this is not an issue im going to go ahead and convert it to a discussion. Let me know if you need anything else. |
Beta Was this translation helpful? Give feedback.
-
@RanVaknin - awaiting for your reply on this. |
Beta Was this translation helpful? Give feedback.
-
Checkboxes for prior research
Describe the bug
Hello,
I am using the
aws-sdk
for Uploading Images to S3 Buckets both Private and public, but recently I started a new Nextjs Project with theaws-sdk
and I got the following Warning in my Terminal:I tried and run the command to migrate to AWS SDK for JavaScript (v3), but I got confused and didn't find proper documentation for Uploading the Images to S3 using the new v3 version SDK, below are my existing v2 SDK Codes, can anyone help me with migrating & get me up with AWS SDK v3 for JS?
The below Code is a reusable Function because I don't want to reinitialize or repeat the same piece of Code and follow DRY.
now I import the below code in the required code as below:
can anyone please help me migrate to AWS SDK for JavaScript (v3) and how to use the new v3 SDK to Upload the Images to S3 as well as to retrieve the Image from Private Bucket on S3?
SDK version number
@aws-sdk/[email protected]
Which JavaScript Runtime is this issue in?
Node.js
Details of the browser/Node.js/ReactNative version
v18.16.1
Reproduction Steps
Migrating SDK Guide From this URL I got a Codemod to run which is as below:
Observed Behavior
it converted the code, but not as I needed or the purpose I need
Expected Behavior
.
Possible Solution
No response
Additional Information/Context
No response
Beta Was this translation helpful? Give feedback.
All reactions