var createApplicationBucket =function () { // Ok, now it might be the first time we run that sample on this account key-pair // and the bucket may not exists yet! so create it now otherwise we will get into trouble later. var sdk =new ForgeSDK.BucketsApi () ; sdk.getBucketDetails (config.bucket, oAuth2TwoLegged, oAuth2TwoLegged.getCredentials ()) .then (function (bucket) { // We are all good! :) console.log (config.bucket, 'present!') ; }) .catch (function (error) { console.error (config.bucket, 'does not exist?', error) ; if ( error.statusCode === 404 ) { console.log ('Creating bucket: ', config.bucket) ; sdk.createBucket ({ bucketKey: config.bucket, policyKey: 'persistent' }, {}, oAuth2TwoLegged, oAuth2TwoLegged.getCredentials ()) .then (function (response) { // We are all good now! :) console.log (config.bucket, 'created! -') ; }) .catch (function (error) { console.error (config.bucket, 'could not be created! - ', error) ; }) ; } else if ( error.statusCode === 403 || error.statusCode === 409 ) { console.error ('This bucket name is already taken by another account, please change it!') ; } else { console.error ('Contact your administrator!') ; } }) ; } ;
///////////////////////////////////////////////////////// // Returns bucket list // ///////////////////////////////////////////////////////// getBuckets (token, opts = {}) { const options = { startAt: opts.startAt || null, region: opts.region || 'US', limit: opts.limit || 100 } return this._bucketsAPI.getBuckets( options, {autoRefresh:false}, token) }
///////////////////////////////////////////////////////// // Creates a new bucket // ///////////////////////////////////////////////////////// createBucket (token, bucketCreationData, opts = {}) { bucketCreationData.bucketKey = validateBucketKey( bucketCreationData.bucketKey) bucketCreationData.policyKey = validatePolicyKey( bucketCreationData.policyKey) const options = { xAdsRegion: opts.xAdsRegion || 'US' } return this._bucketsAPI.createBucket( bucketCreationData, options, {autoRefresh:false}, token) }
.catch (function (error) { console.error (config.bucket, 'does not exist?', error) ; if ( error.statusCode === 404 ) { console.log ('Creating bucket: ', config.bucket) ; sdk.createBucket ({ bucketKey: config.bucket, policyKey: 'persistent' }, {}, oAuth2TwoLegged, oAuth2TwoLegged.getCredentials ()) .then (function (response) { // We are all good now! :) console.log (config.bucket, 'created! -') ; }) .catch (function (error) { console.error (config.bucket, 'could not be created! - ', error) ; }) ; } else if ( error.statusCode === 403 || error.statusCode === 409 ) { console.error ('This bucket name is already taken by another account, please change it!') ; } else { console.error ('Contact your administrator!') ; } })
///////////////////////////////////////////////////////// // Returns bucket details // ///////////////////////////////////////////////////////// getBucketDetails (token, bucketKey) { return this._bucketsAPI.getBucketDetails( bucketKey, {autoRefresh:false}, token) }
///////////////////////////////////////////////////////// // Deletes bucket // ///////////////////////////////////////////////////////// deleteBucket (token, bucketKey) { return this._bucketsAPI.deleteBucket( bucketKey, {autoRefresh:false}, token) }