

# Storing instances with STOW-RS
<a name="dicomweb-storing"></a>

AWS HealthImaging offers a representation of the [https://www.dicomstandard.org/using/dicomweb/store-stow-rs](https://www.dicomstandard.org/using/dicomweb/store-stow-rs) APIs for importing data. Use these APIs to synchronously store DICOM data to your HealthImaging data store.

The following table describes the HealthImaging representations of DICOMweb STOW-RS APIs available for importing data.


**HealthImaging representations of DICOMweb STOW-RS APIs**  

| Name | Description | 
| --- | --- | 
| StoreDICOM | Store one or more instances to a HealthImaging data store. | 
| StoreDICOMStudy | Store one or more instances corresponding to a specified Study Instance UID to a HealthImaging data store. | 

Data imported with the `StoreDICOM` and `StoreDICOMStudy` actions will be organized as new primary image sets, or added to existing primary image sets, using the same logic as asynchronous [import jobs](https://docs.aws.amazon.com/healthimaging/latest/devguide/understanding-import-jobs.html). If the metadata elements of newly imported DICOM P10 data conflict with existing primary [image sets](https://docs.aws.amazon.com/healthimaging/latest/devguide/getting-started-concepts.html#concept-image-set), the new data will be added to non-primary [image sets](https://docs.aws.amazon.com/healthimaging/latest/devguide/getting-started-concepts.html#concept-image-set).

**Note**  
These actions support upload of up to 1GB of DICOM data per request.
The API response will be in the JSON format, conformant to the DICOMweb STOW-RS standard.

**To initiate a StoreDICOM request**  


1. Collect your AWS region, HealthImaging `datastoreId`, and DICOM P10 file name.

1. Construct a URL for the request of the form: `https://dicom-medical-imaging.region.amazonaws.com/datastore/datastore-id/studies`

1. Determine the content length of the DICOM P10 file using your preferred command, for example `$(stat -f %z $FILENAME)`.

1. Prepare and send your request. `StoreDICOM` uses a HTTP POST request with [AWS Signature Version 4](https://docs.aws.amazon.com/IAM/latest/UserGuide/reference_sigv.html) signing protocol.

 

**Example 1: To store a DICOM P10 file using the `StoreDICOM` action**  

```
curl -X POST -v \
  'https://dicom-medical-imaging.us-east-1.amazonaws.com/datastore/d9a2a515ab294163a2d2f4069eed584c/studies' \
  --aws-sigv4 "aws:amz:$AWS_REGION:medical-imaging" \
  --user "$AWS_ACCESS_KEY_ID:$AWS_SECRET_ACCESS_KEY" \
  --header "x-amz-security-token:$AWS_SESSION_TOKEN" \
  --header "x-amz-content-sha256: STREAMING-AWS4-HMAC-SHA256-PAYLOAD" \
  --header "x-amz-decoded-content-length: $CONTENT_LENGTH" \
  --header 'Accept: application/dicom+json' \
  --header "Content-Type: application/dicom" \
  --upload-file $FILENAME
```

**Example 2: To store a DICOM P10 file using the `StoreDICOMStudy` action**  
The only difference between StoreDICOM and StoreDICOMStudy is that a Study Instance UID is passed as a parameter to StoreDICOMStudy, and the uploaded instances must be members of the specified study.  

```
curl -X POST -v \
  'https://dicom-medical-imaging.us-east-1.amazonaws.com/datastore/d9a2a515ab294163a2d2f4069eed584c/studies/1.3.6.1.4.1.5962.1.2.4.20040826285059.5457' \
  --aws-sigv4 "aws:amz:$AWS_REGION:medical-imaging" \
  --user "$AWS_ACCESS_KEY_ID:$AWS_SECRET_ACCESS_KEY" \
  --header "x-amz-security-token:$AWS_SESSION_TOKEN" \
  --header "x-amz-content-sha256: STREAMING-AWS4-HMAC-SHA256-PAYLOAD" \
  --header "x-amz-decoded-content-length: $CONTENT_LENGTH" \
  --header 'Accept: application/dicom+json' \
  --header "Content-Type: application/dicom" \
  --upload-file $FILENAME
```

**Example 3: To store DICOM P10 files with a multi-part HTTP payload**  
Multiple P10 files can be uploaded with a single multi-part upload action. The following shell commands demonstrate how to assemble a multi-part payload containing two P10 files, and upload it with the `StoreDICOM` action.  

```
#!/bin/sh
FILENAME=multipart.payload
BOUNDARY=2a8a02b9-0ed3-c8a7-7ebd-232427531940
boundary_str="--$BOUNDARY\r\n"
mp_header="${boundary_str}Content-Type: application/dicom\r\n\r\n"

##Encapsulate the binary DICOM file 1.
printf '%b' "$mp_header" > $FILENAME
cat file1.dcm >> $FILENAME

##Encapsulate the binary DICOM file 2 (note the additional CRLF before the part header).
printf '%b' "\r\n$mp_header" >> $FILENAME
cat file2.dcm >> $FILENAME

## Add the closing boundary.
printf '%b' "\r\n--$BOUNDARY--" >> $FILENAME

## Obain the payload size in bytes.
multipart_payload_size=$(stat -f%z "$FILENAME")

# Execute CURL POST request with AWS SIGv4
curl -X POST -v \
  'https://iad-dicom.external-healthlake-imaging.ai.aws.dev/datastore/b5f34e91ca734b39a54ac11ea42416cf/studies' \
  --aws-sigv4 "aws:amz:us-east-1:medical-imaging" \
  --user "AKIAIOSFODNN7EXAMPLE:wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY" \
  --header "x-amz-content-sha256: STREAMING-AWS4-HMAC-SHA256-PAYLOAD" \
  --header "x-amz-decoded-content-length: ${multipart_payload_size}" \
  --header 'Accept: application/dicom+json' \
  --header "Content-Type: multipart/related; type=\"application/dicom\"; boundary=\"${BOUNDARY}\"" \
  --data-binary "@$FILENAME"

# Delete the payload file
rm $FILENAME
```