Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 29 additions & 36 deletions guides/integration/platform/attachments.md
Original file line number Diff line number Diff line change
@@ -1,55 +1,48 @@
---
description: >
How to integrate the SAP Business Technology Platform Attachment Service to manage file attachments in CAP applications.
How to add file attachment handling to CAP applications using the @cap-js/attachments plugin.
---

# Adding Attachments to Your CAP Application
# Attachments

You can use the Attachment Service provided by SAP Business Technology Platform to manage file attachments in your CAP applications. This guide explains how to integrate the Attachment Service into your CAP application.
The [`@cap-js/attachments`](https://github.com/cap-js/attachments) plugin adds file storage and handling to CAP applications via a reusable `Attachments` aspect. In development it stores files in the local database; in production it uses an Object Store service (AWS S3, Azure Blob Storage, or GCP Cloud Storage).

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Clarity]: The introduction uses a semicolon and a long sentence

The style guide asks to avoid semicolons, and the current sentence combines development and production behavior in one line. Consider splitting it into shorter sentences and adding the comma after the introductory phrase.

Suggested change
The [`@cap-js/attachments`](https://github.com/cap-js/attachments) plugin adds file storage and handling to CAP applications via a reusable `Attachments` aspect. In development it stores files in the local database; in production it uses an Object Store service (AWS S3, Azure Blob Storage, or GCP Cloud Storage).
The [`@cap-js/attachments`](https://github.com/cap-js/attachments) plugin adds file storage and handling to CAP applications with a reusable `Attachments` aspect. In development, it stores files in the local database. In production, it uses an Object Store service (AWS S3, Azure Blob Storage, or GCP Cloud Storage).

Double-check suggestion before committing. Edit this comment for amendments.


Please provide feedback on the review comment by checking the appropriate box:

  • 🌟 Awesome comment, a human might have missed that.
  • ✅ Helpful comment
  • 🤷 Neutral
  • ❌ This comment is not helpful


## Prerequisites
## Setup

- A CAP project set up on SAP Business Technology Platform.
- Access to the SAP Business Technology Platform Cockpit.
- Basic knowledge of CAP and Node.js.
Add the package to your project:

## Steps to Integrate Attachment Service
```sh
npm add @cap-js/attachments
```

1. **Enable Attachment Service**: In the SAP Business Technology Platform Cockpit, navigate to your subaccount and enable the Attachment Service.
The plugin configures itself automatically.

2. **Install Required Packages**: In your CAP project, install the necessary packages for working with attachments. You can use the following command:
## Adding Attachments to Your Model

```bash
npm install @sap/cds-srv-attachments
```
Import the `Attachments` aspect and add a composition to your entity:

3. **Configure Attachment Service**: In your `package.json` file, add the Attachment Service configuration under the `cds` section:
```cds
using { Attachments } from '@cap-js/attachments';

```json
"cds": {
"requires": {
"attachments": {
"kind": "attachment-service"
}
}
}
```
entity Incidents : cuid {
//...
attachments : Composition of many Attachments;
}
```

4. **Define Attachment Entity**: In your CDS model, define an entity for attachments. For example:
To get the Fiori elements attachment UI, the entity must be draft-enabled:

```cds
entity Attachments {
key ID : UUID;
Name : String;
Content: LargeBinary;
MimeType: String;
}
```
```cds
service IncidentsService {
entity Incidents as projection on my.Incidents;
annotate Incidents with @odata.draft.enabled;
Comment on lines +37 to +38

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Clarity]: The draft annotation example is hard to follow

The annotation is indented as if it belongs to the projection, and my.Incidents is not introduced in the snippet, which can confuse readers who copy the example. Consider annotating the projection directly and keeping the model reference consistent with the previous snippet.

Suggested change
entity Incidents as projection on my.Incidents;
annotate Incidents with @odata.draft.enabled;
@odata.draft.enabled
entity Incidents as projection on Incidents;

Double-check suggestion before committing. Edit this comment for amendments.


Please provide feedback on the review comment by checking the appropriate box:

  • 🌟 Awesome comment, a human might have missed that.
  • ✅ Helpful comment
  • 🤷 Neutral
  • ❌ This comment is not helpful

}
```

5. **Implement Attachment Logic**: In your service implementation file (for example, `srv/your-service.js`), implement the logic to handle attachment operations such as upload, download, and delete.
## Production Setup

6. **Test Your Application**: Run your CAP application and test the attachment functionality to ensure everything is working as expected.
For Cloud Foundry, bind an Object Store service instance to your application and include it in your `mta.yaml`. The plugin picks up the binding and uses it as the storage backend.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Bug]: The production setup omits the required attachments configuration

Binding the Object Store instance alone may not select the object-store backend; CAP examples configure cds.requires.attachments.kind as standard or a hyperscaler-specific kind. Consider adding the package configuration so readers can deploy the setup reliably.

Suggested change
For Cloud Foundry, bind an Object Store service instance to your application and include it in your `mta.yaml`. The plugin picks up the binding and uses it as the storage backend.
For Cloud Foundry, bind an Object Store service instance to your application and include it in your `mta.yaml`. Configure the attachments service in `package.json` so the plugin can use the binding as the storage backend:
```json
{
"cds": {
"requires": {
"attachments": {
"kind": "standard"
}
}
}
}
```

Double-check suggestion before committing. Edit this comment for amendments.


Please provide feedback on the review comment by checking the appropriate box:

  • 🌟 Awesome comment, a human might have missed that.
  • ✅ Helpful comment
  • 🤷 Neutral
  • ❌ This comment is not helpful


## Conclusion
## Further Capabilities

By following these steps, you can successfully integrate the Attachment Service into your CAP application, allowing you to manage file attachments efficiently. For more detailed information, refer to the official SAP documentation on the Attachment Service.
The plugin also supports file size and MIME type restrictions, malware scanning via SAP's malware scanning service, audit logging, and programmatic attachment copying between records. See the [plugin repository](https://github.com/cap-js/attachments) for details.
Loading