0
0
DynamodbConceptBeginner · 3 min read

Binary Type in DynamoDB: What It Is and How to Use It

In DynamoDB, the Binary type stores any kind of binary data such as images, files, or encrypted content as a sequence of bytes. It allows you to save raw data directly without converting it to text. This type is useful when you need to store non-text information efficiently.
⚙️

How It Works

The Binary type in DynamoDB lets you store data as raw bytes, similar to how you might save a photo or a document on your computer. Think of it like a sealed envelope that holds any kind of digital content without changing it.

When you save binary data, DynamoDB keeps it exactly as you provide it, so you can retrieve the same data later without loss or modification. This is different from text data, which is stored as readable characters.

Using binary type is like having a special box for fragile items—you keep the data safe and intact, ready to be used exactly as it was stored.

💻

Example

This example shows how to store and retrieve binary data in DynamoDB using AWS SDK for JavaScript (v3). It saves a small image as binary data and then reads it back.

javascript
import { DynamoDBClient, PutItemCommand, GetItemCommand } from "@aws-sdk/client-dynamodb";

const client = new DynamoDBClient({ region: "us-east-1" });

async function run() {
  // Example binary data (a small byte array)
  const binaryData = new Uint8Array([137, 80, 78, 71]); // PNG file signature bytes

  // Put item with binary attribute
  await client.send(new PutItemCommand({
    TableName: "MyTable",
    Item: {
      "Id": { S: "image1" },
      "ImageData": { B: binaryData.buffer }
    }
  }));

  // Get item back
  const result = await client.send(new GetItemCommand({
    TableName: "MyTable",
    Key: { "Id": { S: "image1" } }
  }));

  console.log("Retrieved binary data:", result.Item.ImageData.B);
}

run();
Output
Retrieved binary data: Uint8Array(4) [137, 80, 78, 71]
🎯

When to Use

Use the Binary type when you need to store data that is not text, such as images, audio files, encrypted data, or any other raw bytes. It is perfect for applications that handle media files or secure information.

For example, a photo-sharing app might store user-uploaded pictures as binary data in DynamoDB. Similarly, an app that encrypts sensitive information before saving it can use binary type to keep the encrypted bytes intact.

Key Points

  • Binary type stores raw bytes exactly as provided.
  • Useful for images, files, and encrypted data.
  • Data is not converted to text, preserving integrity.
  • Works well with AWS SDKs that support binary attributes.

Key Takeaways

DynamoDB's binary type stores raw byte data like images or encrypted files without conversion.
Use binary type when you need to save non-text data safely and retrieve it unchanged.
AWS SDKs support binary attributes for easy storing and reading of binary data.
Binary data is stored exactly as provided, preserving its original form.
Ideal for media apps, encryption, and any use case requiring raw data storage.