# File Storage and Default S3 Bucket

## Basic Concept

### Standard Bucket

Our standard approach at InsureMO for document storage during tenant setup is as follows:

* Each tenant is provided with a standard tenant bucket. For example, for the UAT tenant, the bucket format will be `tenant-sg-uat-bucket`. Each tenant bucket will have a unique physical endpoint address. 

* The standard tenant bucket functions when the BFF (Backend for Frontend) calls the standard platform API. For example, the BFF may call the DMS API for document management or when the project team needs to deploy configuration data and temporarily store the configuration file.

* If your project team requires detailed bucket information, please ask the InsureMO SiteOps team for help.

### Storage Method

The project team has three options for storing content in the BFF:

* For technical purposes, call the Container CloudDisk API. This allows you to configure the bucket and store documents in any location. It is more suitable for technical scenarios such as file integration. For guidance, see [CloudDisk-Service](https://docs.insuremo.com/non_insurance_service/docs/md/clouddisk/InsureMO_clouddisk_bucket).

* For business purposes, utilize the ICS DMS API (not the filesupport JAR mentioned in this document). This API facilitates saving files to the tenant’s standard bucket. It is commonly used for business documents, such as policy document, as it offers business ID indexing. 

* For SAAS purposes, adopt the InsureMO iDocs offering, which provides the entire UI journey for document management.

| Characteristics | DMS                                                                 | CloudDisk                                                                            | iDocs                                                                            |
|---------------|---------------------------------------------------------------------|--------------------------------------------------------------------------------------|-------------------------------------------------------------------------------------|
| Position      | PAAS                                                                | PAAS                                                                                 | SAAS                                                                                |
| Support Team  | Appframework                                                        | Container                                                                            | iDocs                                                                            |
| Purpose       | Business doc management                                             | Technical file management                                                            | Business doc management                                                             |
| With UI       | No                                                                  | No                                                                                   | Yes                                                                                 |
| Subscription  | InsureMO                                                            | InsureMO                                                                             | Separate                                                                            |
| Reference     | [Document Management](https://docs.insuremo.com/ics/app_framework/dms) | [CloudDisk-Service](https://docs.insuremo.com/non_insurance_service/docs/md/clouddisk/InsureMO_clouddisk_welcome) | [iDocs Introduction](https://docs.insuremo.com/offerings_hub/iDocs_introduction_simplified) |


## Bucket Naming Rule

For each environment, a common bucket will be set up for each tenant. The bucket format is `{tenant code}-{env bucket prefix}bucket`, e.g., `TenantCode-vela-smk-bucket`, `freshman-ptdev-bucket`.  

Within each bucket, separate folders are created underneath, typically categorized as follows:


|            Folder Name            | Level  | Creator | Usage                      | Sample |
| ------------------------------- | ---- | -----  | ------------------------- |--------------- |
| `{tenant code}-{env bucket prefix}bucket-template`   | tenant |   TS    | Batch, product, plan, pp  |`TenantCode-vela-smk-bucket-template`|
| `{tenant code}-{env bucket prefix}bucket-instance`   | tenant |   TS    | Ratetable upload, upload  |`TenantCode-vela-smk-bucket-instance`|
| `{tenant code}-{env bucket prefix}bucket-dms-thumb`   | tenant |   TS    |  DMS storage              |`TenantCode-vela-smk-bucket-dms-thumb`|
| `{tenant code}-{env bucket prefix}bucket-dms-document` | tenant |   TS    |  DMS thumbs                 |`TenantCode-vela-smk-bucket-dms-document`|
| `{tenant code}-{env bucket prefix}bucket-platform`   | tenant |   TS    | Data I/O,  lib product copy |`TenantCode-vela-smk-bucket-platform`|


## Share Bucket

If the project team has specific usage requirements and wants to share the bucket with the client's IT team, for instance, exposing the bucket to the client's team to fetch data like batch printing PDFs, we recommend setting up a separate bucket. This approach ensures:

* A clean new bucket, free from any temporary files in the platform bucket.
* Easy control over both read-only and editable authorities.

Depending on the volume of business transactions and security considerations, buckets can be set up under either a client’s account or an InsureMO account.

It is preferable to set buckets up under a client’s account to reduce S3 costs (storage and transmission fees) and address security concerns. However, if data security and regulatory compliance are the primary concerns, it is recommended that the bucket be set up under an InsureMO account. 


## Environment Parameter (for Private Cloud)

### Global

|         Parameter Name                  |      Usage      |       Sample Value                  | Remark |
| ------------------------------------- | ------------ | --------------------------------- | ---- |
|           `file.bucket.prefix`            |   Append bucket   | vela-dev-, vela-smk-              | Necessary |
|        `file.supportType.default`         |    Storage type    | S3, Azure, Aliyun, Sftp, SystemFile | Necessary |
| `platform.foundation.file.type.whiteList` | File extension whitelist | .csv, .xls, .xlsx    | Necessary |


### For S3

|   Parameter Name          | Usage |   Sample Value    |          Remark           |
| ------------------------------ | -- | ------------ | --------------------- |
| `{tenant}.file.s3client.accessKey` | Connection |      -         |        Necessary for tenants         |
| `{tenant}.file.s3client.secretKey` | Connection |      -         |        Necessary for tenants        |
|  `{tenant}.file.s3client.region`   | Connection | us-east-1     |        Necessary for tenants         |
| `{tenant}.file.s3client.endpoint`  | Connection |    -           | Unnecessary（default as an AWS server） |

### For Azure

|                Parameter Name         | Usage |              Default Value              |           Remark            |
| ----------------------------------- | ---- | ---------------------------------- | ------------------------- |
| `{tenant}.file.azureClient.accountName` | Connection |                        -          |          Necessary for tenants          |
| `{tenant}.file.azureClient.accountKey`  | Connection |                    -              |          Necessary for tenants          |
| `{tenant}.file.azureClient.endpoint`       | Connection | `https://%s.blob.core.windows.net` | Not Necessary（Default Azure server） |

### For Aliyun

|              Parameter Name       | Usage |               Sample               |   Remark   |
|  -------------------------------  | ----- | ---------------------------------- | ------ |
| `{tenant}.file.ossClient.accessKey` | Connection |          -                            | Necessary for tenants |
| `{tenant}.file.ossClient.secretKey` | Connection |         -                             | Necessary for tenants |
| `{tenant}.file.ossClient.endpoint`  | Connection | `https://oss***aliyuncs.com` | Necessary for tenants |

### For SFTP

|           Parameter Name    | Usage | Default Value |   Remark   |
| -------------------------- | ---- | ---- | ------ |
|   `{tenant}.file.sftp.host`   | Connection |  -      | Necessary for Tenant |
|   `{tenant}.file.sftp.port`   | Connection |   22   | Necessary for Tenant |
| `{tenant}.file.sftp.username` | Connection |   -     | Necessary for Tenant |
| `{tenant}.file.sftp.password` | Connection |    -    | Necessary for Tenant |
| {tenant}.sftp.mount.path | Storge |  | The custom root folder path |

### For Google

| Parameter Name                          | Usage      | Default Value | Remark               |
| --------------------------------------- | ---------- | ------------- | -------------------- |
| {tenant}.file.googleClient.clientId     | Connection |               | Necessary for Tenant |
| {tenant}.file.googleClient.clientEmail  | Connection |               | Necessary for Tenant |
| {tenant}.file.googleClient.privateKey   | Connection |               | Necessary for Tenant |
| {tenant}.file.googleClient.privateKeyId | Connection |               | Necessary for Tenant |

### For Custom

Custom storage, You can store files and operate it in customized storage media through API

To enable this function, here is the steps

1. by passing field `customAppName` in `AbstractFileRequest`
2. implement the interface `CustomFileSupportRest`

**note: It is recommended to use this function through  DMS **

#### POM Dependency

```xml
 			<dependency>
                <groupId>com.insuremo.vela</groupId>
                <artifactId>vela-file-support</artifactId>
                <version>${project.version}</version>
            </dependency>
```



#### Method Summary

| Modifier And Type     | Method                                | Return             | Description                                                  |
| --------------------- | ------------------------------------- | ------------------ | ------------------------------------------------------------ |
| public FileSaveResult | save(FileSaveRequest request)         | FileSaveResult     | adds an object                                               |
| public byte[]         | get(FileGetRequest request)           | byte[] of object   | retrieves an object                                          |
| public void           | delete(FileDeleteRequest request)     |                    | remove  an object from a bucket                              |
| public Object         | listVersion(FileGetRequest request)   | List ObjectVersion | get all versions of the object in a bucket                   |
| public Long           | getObjectSize(FileGetRequest request) | the object size    | get the object size from an object without returning the object itself |
| public void           | copy(FileCopyRequest request)         |                    | creates a copy of an object that is already stored in a bucket |

##### FileSaveRequest 

Used in save() method

| Field     | Description           | Required |
| --------- | --------------------- | -------- |
| objectKey | The key of the object | Y        |
| bytes[]   | bytes of  the object  | Y        |

##### FileSaveResult

Return of save() method

| Field     | Description            | Type | Required |
| --------- | ---------------------- | ---- | -------- |
| versionId | version id of anobject | Long | N        |

##### FileGetRequest

Used in get(), listVersion(), getObjectSize() method

| Field     | Description             | Required |
| --------- | ----------------------- | -------- |
| objectKey | The key of the object   | Y        |
| versionId | version id of an object | N        |

##### FileDeleteRequest

Used in delete() method

| Field     | Description           | Required |
| --------- | --------------------- | -------- |
| objectKey | The key of the object | Y        |

##### FileCopyRequest

Use in copy() method

| Field                | Description                       | Required |
| -------------------- | --------------------------------- | -------- |
| sourceObjectKey      | The key of the object to copy     | Y        |
| destinationObjectKey | The key of the destination object | Y        |

##### ObjectVersion

Return of listVersion() method

| Field        | Description                                                  | Type    | Requried |
| ------------ | ------------------------------------------------------------ | ------- | -------- |
| key          | the key of the object                                        | String  | N        |
| eTag         | The entity tag is an MD5 hash of that version of the object. | String  | N        |
| size         | Size in bytes of the object.                                 | Long    | N        |
| versionId    | version id of an object                                      | String  | N        |
| isLatest     | Specifies whether the object is (true) or is not (false) the latest version of an object. | Boolean | N        |
| lastModified | Date and time when the object was last modified.             | Instant | N        |

