Skip to content

Instantly share code, notes, and snippets.

@harshavardhana
Last active January 29, 2023 01:33
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save harshavardhana/9f8f2236dee975fb70edbec5db14fd67 to your computer and use it in GitHub Desktop.
Save harshavardhana/9f8f2236dee975fb70edbec5db14fd67 to your computer and use it in GitHub Desktop.
Upload to play.minio.io:9000 from browser using minio-js
<!DOCTYPE html>
<html>
<head>
<title>Minio SDK for JavaScript - Sample Application</title>
<script src="./dist/main/minio-browser.js"></script>
</head>
<body>
<div id="results"></div>
<input type="file" id="file-chooser" />
<button id="upload-button">Upload to Minio</button>
<script type="text/javascript">
var s3 = new Minio.Client({
accessKey: 'Q3AM3UQ867SPQQA43P2F',
secretKey: 'zuf+tfteSlswRu7BJ86wekitnifILbZam1KYY3TG',
endPoint: 'play.minio.io',
secure: true,
port: 9000
});
var fileChooser = document.getElementById('file-chooser');
var button = document.getElementById('upload-button');
var results = document.getElementById('results');
var reader = new FileReader();
button.addEventListener('click', function () {
var file = fileChooser.files[0];
if (file) {
results.innerHTML = '';
// Object key will be facebook-USERID#/FILE_NAME
var objKey = 'myobjects' + '/' + file.name;
var params = {
Bucket: 'testbucket',
Key: objKey,
ContentType: file.type,
Body: file,
};
reader.onloadend = function () {
s3.putObject('testbucket', objKey, reader.result, file.type, function(e) {
if (e) {
results.innerHTML = 'ERROR: ' + e;
} else {
s3.statObject('testbucket', objKey, function(e, stat) {
if (e) {
results.innerHTML = 'ERROR: ' + e;
} else {
results.innerHTML = objKey + ' Size:' + stat.size + ' ETag:' + stat.etag;
}
});
}
});
}
reader.readAsText(file);
} else {
results.innerHTML = 'Nothing to upload.';
}
}, false);
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment