# Google-cloud

### Upload file to google-cloud&#x20;

### &#x20;1- Finish this guide

{% content-ref url="../create-upload-service" %}
[create-upload-service](https://feathersjs-mongoose.gitbook.io/feathers-mongoose-casl/version-2-support-feathers-4/docs/guides/upload-files/create-upload-service)
{% endcontent-ref %}

###

### 2- Create google-cloud account

1. Follow this 3 first steps of this [guide](https://medium.com/@iwozzy/easily-host-images-with-node-and-google-cloud-storage-29fb14e2cdb8)
2. Create secret-files folder  in the src folder
3. copy the JSON key  file from google-cloud to src > secret-files and rename the file to secret-files.json

###

### 3 - new bucket

1. &#x20;Create new bucket<br>

   ![](https://1684234023-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LY13uPlo20E7hNPj1d2%2F-LjTymPBWFdfAezHSRyd%2F-LjU-GpBpz1EZXSE7EYa%2Fgoogle-cloud-bucket.jpg?alt=media\&token=4239cab7-fc9a-46e9-8f45-331dcccff4ab)

###

### 4- Update config json

**path**: config > default.json<br>

1. Update projectId and bucket name<br>

   ```javascript
   "google-cloud": {
       "projectId": "THIS IS THE PROJECT NAME",
       "bucket": "THE BOCKET NAME",
       "keyFilename": "../src/secret-files/google-key.json",
       "signedUrlExpires" : 900
     },
   ```
2. Allow google-cloud<br>

   ```javascript
     "feathers-mongoose-casl": {
       "uploads": {
         "services": {
           "s3": false,
           "local-private": true,
           "local-public": true,
           "google-cloud": true // This need to be true
         }
         ....
   ```

### **5- Update upload middleware configuration**

**path:** src > services > \[YOUR\_SERVICE\_NAME] > \[YOUR\_SERVICE\_NAME].service.js

```javascript
  app.use('/organizations-files',
    uploadMiddleware({
      app,
      fileKeyName: 'file',
      serviceName: 'YOUR_SERVICE_NAME',
      storageService: STORAGE_TYPES['google-cloud'], // That's the change we made
      publicAcl: false,
      // mimetypes: ['image/png','image/jpeg'] // optional
    }),
    createService(options)
  );
```

### 6- Update service validators

**path**: src > validators >  \[YOUR\_SERVICE\_NAME].validators.js

```javascript
const {Joi, enums} = require('feathers-mongoose-casl');

const getJoiObject = function(withRequired) {
  const required = withRequired ? 'required' : 'optional';
    return Joi.object({
    storage: Joi.string().valid(
      enums.STORAGE_TYPES['google-cloud'], //We need to add this line
      enums.STORAGE_TYPES['others'], // When user pass link to file
      ).meta({ dashboard: { hide: 1 }})
    ...
    })
}
```

####

### 7- Update service hooks

**path:** src > services > \[YOUR\_SERVICE\_NAME] > \[YOUR\_SERVICE\_NAME].hooks.js

```javascript
const {hooks} = require('feathers-mongoose-casl');
const {uploadsHooks} = hooks;

const uploadHookConfig = {
  serviceName: 'YOUR_SERVICE_NAME',
  fileKeyName: 'file',
  singUrlKeyName: 'file',
  privateFile: true,
  autoSignUrl: true,
  userKeyName: 'user'
};


module.exports = {
  before: {
    all: [uploadsHooks(uploadHookConfig)],
    find: [],
    get: [],
    create: [],
    update: [],
    patch: [],
    remove: []
  },

  after: {
    all: [uploadsHooks(uploadHookConfig)],
    find: [],
    get: [],
    create: [],
    update: [],
    patch: [],
    remove: []
  },
}
```

### 8 - Done!

test the service

test from dashboard :\
1\) open [feathersjs-mongoose-casl-admin](https://feathersjs-mongoose.gitbook.io/feathers-mongoose-casl/version-2-support-feathers-4/docs/start-new-project-1/install-feathers-mongoose-casl) and try to upload a file
