New things for GCS Adapter
This commit is contained in:
@@ -1,4 +1,3 @@
|
|||||||
|
|
||||||
var FilesController = require('../src/Controllers/FilesController').FilesController;
|
var FilesController = require('../src/Controllers/FilesController').FilesController;
|
||||||
var Config = require("../src/Config");
|
var Config = require("../src/Config");
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
export function loadAdapter(adapter, defaultAdapter, options) {
|
export function loadAdapter(adapter, defaultAdapter, options) {
|
||||||
|
|
||||||
if (!adapter)
|
if (!adapter)
|
||||||
{
|
{
|
||||||
if (!defaultAdapter) {
|
if (!defaultAdapter) {
|
||||||
|
|||||||
@@ -4,18 +4,33 @@ import { storage } from 'gcloud';
|
|||||||
import { FilesAdapter } from './FilesAdapter';
|
import { FilesAdapter } from './FilesAdapter';
|
||||||
import requiredParameter from '../../requiredParameter';
|
import requiredParameter from '../../requiredParameter';
|
||||||
|
|
||||||
|
function requiredOrFromEnvironment(env, name) {
|
||||||
|
let environmentVariable = process.env[env];
|
||||||
|
if (!environmentVariable) {
|
||||||
|
requiredParameter(`GCSAdapter requires an ${name}`);
|
||||||
|
}
|
||||||
|
return environmentVariable;
|
||||||
|
}
|
||||||
|
|
||||||
|
function fromEnvironmentOrDefault(env, defaultValue) {
|
||||||
|
let environmentVariable = process.env[env];
|
||||||
|
if (environmentVariable) {
|
||||||
|
return environmentVariable;
|
||||||
|
}
|
||||||
|
return defaultValue;
|
||||||
|
}
|
||||||
|
|
||||||
export class GCSAdapter extends FilesAdapter {
|
export class GCSAdapter extends FilesAdapter {
|
||||||
// GCS Project ID and the name of a corresponding Keyfile are required.
|
// GCS Project ID and the name of a corresponding Keyfile are required.
|
||||||
// Unlike the S3 adapter, you must create a new Cloud Storage bucket, as this is not created automatically.
|
// Unlike the S3 adapter, you must create a new Cloud Storage bucket, as this is not created automatically.
|
||||||
// See https://googlecloudplatform.github.io/gcloud-node/#/docs/master/guides/authentication
|
// See https://googlecloudplatform.github.io/gcloud-node/#/docs/master/guides/authentication
|
||||||
// for more details.
|
// for more details.
|
||||||
constructor(
|
constructor(
|
||||||
projectId = requiredParameter('GCSAdapter requires a GCP Project ID'),
|
projectId = requiredOrFromEnvironment('GCP_PROJECT_ID', 'projectId'),
|
||||||
keyFilename = requiredParameter('GCSAdapter requires a GCP keyfile'),
|
keyFilename = requiredOrFromEnvironment('GCP_KEYFILE_PATH', 'keyfile path'),
|
||||||
bucket = requiredParameter('GCSAdapter requires a GCS bucket name'),
|
bucket = requiredOrFromEnvironment('GCS_BUCKET_NAME', 'bucket name'),
|
||||||
{ bucketPrefix = '',
|
{ bucketPrefix = fromEnvironmentOrDefault('GCS_BUCKET_PREFIX', ''),
|
||||||
directAccess = false } = {}
|
directAccess = fromEnvironmentOrDefault('GCS_DIRECT_ACCESS', false) } = {}) {
|
||||||
) {
|
|
||||||
super();
|
super();
|
||||||
|
|
||||||
this._bucket = bucket;
|
this._bucket = bucket;
|
||||||
|
|||||||
@@ -27,6 +27,8 @@ export class FilesController extends AdaptableController {
|
|||||||
filename = randomHexString(32) + '_' + filename;
|
filename = randomHexString(32) + '_' + filename;
|
||||||
|
|
||||||
var location = this.adapter.getFileLocation(config, filename);
|
var location = this.adapter.getFileLocation(config, filename);
|
||||||
|
console.log(this.adapter);
|
||||||
|
console.log(location);
|
||||||
return this.adapter.createFile(config, filename, data, contentType).then(() => {
|
return this.adapter.createFile(config, filename, data, contentType).then(() => {
|
||||||
return Promise.resolve({
|
return Promise.resolve({
|
||||||
url: location,
|
url: location,
|
||||||
|
|||||||
Reference in New Issue
Block a user