मैं अपने एस 3 बाल्टी में एक एडब्ल्यूएस निर्धारित-यूआरएल के साथ एक फाइल अपलोड करने की कोशिश कर रहा हूं।
यहां मेरा जेएस फ़ंक्शन है
function UploadObjectUsingPresignedURL()
{
var file = document.getElementById('customFile').files[0];
console.log(file);
var xhr = new XMLHttpRequest();
xhr.open('PUT', 'hereMyPresignedURL', true);
//xhr.setRequestHeader('Content-Type', 'image/jpeg');
xhr.onload = () => {
if (xhr.status === 200) {
console.log('Uploaded data successfully');
}
};
xhr.onerror = () => {
console.log('Nope')
};
xhr.send(file); // `file` is a File object here
}
यहाँ मेरा एचटीएमएल
<div class="input-group">
<div class="custom-file">
<input type="file" class="custom-file-input" id="customFile"
aria-describedby="customFile">
<label class="custom-file-label" for="customFile">Choose file</label>
</div>
<div class="input-group-append">
<button class="btn btn-outline-secondary" type="button" id="customFile"
onclick="UploadObjectUsingPresignedURL()">Button</button>
</div>
</div>
और यहाँ मेरे कंसोल में परिणाम है ...
- functions.js:4 फ़ाइल {नाम: "aragorn.jpg", lastModified: 1590136296908, lastModifiedDate: शुक्र मई 22 2020 10:31:36 GMT+0200 (Heure d'été d'Europe Centrale), webkitRelativePath: "", size : ७७१४, …}
- functions.js:10 डेटा सफलतापूर्वक अपलोड किया गया)
तो ऐसा लगता है कि यह अच्छी तरह से चला गया (डेटा सफलतापूर्वक अपलोड किया गया), लेकिन मेरी एस 3 बाल्टी में मेरे पास कोई नई फाइल नहीं है ... चूंकि मुझे कोई त्रुटि नहीं है, मुझे नहीं पता कि इसे कैसे डिबग करना है ...
धन्यवाद :) !!
संपादित करें : यहाँ मेरा .NET कोड Presigned-URL उत्पन्न करने के लिए है:
public static string MakeS3PresignedURIUpload()
{
string awsAccessKeyId = "XXX";
string awsSecretAccessKey = "XXX";
string s3Bucket = "test-bucket";
string s3Key = "/aragorn.jpg";
string awsRegion = "us-west-2";
string presignedUri = "Error : No S3 URI found!";
int expirationMinutes = 60;
if (s3Bucket != String.Empty && s3Key != String.Empty && awsRegion != String.Empty)
{
try
{
s3Key = s3Key.Replace("\\", "/");
GetPreSignedUrlRequest presignedUriReq = new GetPreSignedUrlRequest();
RegionEndpoint myRegion = RegionEndpoint.GetBySystemName(awsRegion);
AmazonS3Client client = new AmazonS3Client(awsAccessKeyId, awsSecretAccessKey, myRegion);
presignedUriReq.Verb = HttpVerb.PUT;
presignedUriReq.BucketName = s3Bucket;
presignedUriReq.Key = s3Key;
presignedUriReq.Expires = DateTime.UtcNow.AddMinutes(expirationMinutes);
presignedUri = client.GetPreSignedURL(presignedUriReq);
}
catch (AmazonS3Exception e)
{
return string.Format("Error : S3 - MakeS3PresignedURIUpload - Error encountered on server.Message:'{0}' when writing an object", e.Message);
}
catch (Exception e)
{
return string.Format("Error : S3 - MakeS3PresignedURIUpload - Unknown encountered on server. Message:'{0}' when writing an object", e.Message);
}
}
return presignedUri;
}
2 जवाब
string s3Key = "/aragorn.jpg";
में बदलो
string s3Key = "aragorn.jpg";
यह काम कर रहा था, लेकिन फ़ाइल को बाल्टी में एक अज्ञात फ़ोल्डर में सहेज रहा था और सीधे बाल्टी में नहीं ...
धन्यवाद @wschopohl मेरी मदद करने के लिए :)!
उपयोगी लिंक: निर्धारित URL का उपयोग करके Amazon S3 बकेट में फ़ाइल अपलोड करें
निर्धारित URL का उपयोग करके Amazon S3 बकेट में फ़ाइल अपलोड करें